Script not updating except under strange conditions.

Basically, I have a FindAndTrackEnemies script attached to my enemy entities. The problem is that a function in this script only seems to execute the first few lines except when the player spawns an attack prefab. A Debug.Log() at the top of aiDecision() (the function in question) does update in the console, however, using more Debug.Logs, it appears that the script only executes the first few lines as the others don’t register after a point. Could a null reference error or some other case cause Unity not to finish a function? I do get these with functions being called in that script, tho this seems unlikely as other functions have given me this error and have still executed to completion. Also, these functions shouldn’t even have that error based on their execution. For example, this function delivers that error…

function getTargetDistance (target : Transform) : float {
    var distance : float = Vector3.Distance(transform.position, target.position);
    return distance;   
}

Any thoughts would be appreciated!!! Thanks in advance.

Yes, a null reference makes the function drop out at that point which can be quite annoying.

I’d change your function to check ‘target’ is definitely not null before returning a distance.

1 Like

And if target is null simply return an impossibly large value, or a negative one. Something that will let the calling method know there is no target.

1 Like

Thanks, this solved the problem :slight_smile: My enemies only tracked when I attacked because without any player attacks existing, my script would drop out from the null reference error.

And for the tip BoredMormon!