Hi all,
I’m working in C#
I have an enemy game object with a script attached. This script has a public GameObject variable called target.
The enemy game object is meant to go towards the target in a linear fashion.
The target gets spawned, the enemy gets its target set to the newly spawned target but it doesn’t go anywhere.
In fact the target it’s tracking is null.
I have a script called SceneController. in this script there is a spawn method like so:
void DoSpawnPlayer()
{
GameObject playerObject = playerSpawner.DoSpawnPlayer();
foreach(GameObject threatObject in threatObjects)
{
Threat threat = threat.GetComponent<Threat>();
threat.target = playerObject ;
}
}
In the threat I have a method called in Update called MoveTowardsTarget
public void MoveTowardsTarget()
{
Debug.Log("target: " + target + " self position: " + transform.position);
MoveTowardsTarget(target);
}
Now… the target variable in the Enemy script is always null, but in the SceneController it reports it back as being not null.
I’ve checked to see if there is another instance of the Enemy in the game and there appears to only be ONE.
I’ve printed the position of the Enemy in both the Enemy script and the SceneController script and they are completely different.
I don’t understand what’s wrong.
The Enemy and Spawn location were dropped in as a prefab but the player is instantiated.
I hope I’m clear with my explanation