Hello guys.
Me and my friends have had this anomalous issue when trying to use this script in our code.
When we try and run the code, we get the error:
error CS0029: Cannot implicitly convert type 'UnityEngine.Transform' to 'UnityEngine.GameObject'
...despite the fact that the function is question does not require a GameObject input.
Any help would be 1000x appreciated. This is a massive roadblock for us.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NewCopScript : MonoBehaviour
{
// Objectives: New Cop Script with Few Variables, and one that actually works.
public NavMeshAgent Perry;
public List Positions = new List();
public GameObject Bullet;
public float MaxHealth;
public float Health;
public List Enemies = new List();
public List EnemyTransforms = new List();
public GameObject ClosestEnemy;
// Start is called before the first frame update
void Start()
{
Health = MaxHealth;
}
// Update is called once per frame
void Update()
{
CalculateEnemies();
Move();
}
// Runs Movement
void Move() {
if (Perry.hasPath == false) {
Transform Position = Positions[Random.Range(0, Positions.Count)];
Perry.SetDestination(new Vector3(Position.position.x,Position.position.y,Position.position.z));
}
}
// Runs Shooting
void Shoot() {
}
// Calculates Enemy Positions for GetClosestEnemy
void CalculateEnemies() {
int I = 0;
EnemyTransforms.Clear();
Enemies.Clear();
Enemies.AddRange(GameObject.FindGameObjectsWithTag("Zombie"));
foreach (GameObject Enemy in Enemies) {
EnemyTransforms.Insert(I, Enemy.transform);
I++;
}
ClosestEnemy = GetClosestEnemy(EnemyTransforms, this.transform);
}
// Runs Tracking for Zombies
Transform GetClosestEnemy (List enemy, Transform fromThis) {
Transform bestTarget = null;
float closestDistanceSqr = Mathf.Infinity;
Vector3 currentPosition = fromThis.position;
foreach(Transform potentialTarget in enemy) {
if (potentialTarget != null) {
Vector3 directionToTarget = potentialTarget.position - currentPosition;
float dSqrToTarget = directionToTarget.sqrMagnitude;
if (dSqrToTarget < closestDistanceSqr) {
closestDistanceSqr = dSqrToTarget;
bestTarget = potentialTarget;
}
}
}
return bestTarget;
}
}
↧