How can I make the player the AI target only if he is alive?

I’ve posted about this recently, but didn’t get any responses. I’m hoping I might if I simplify my question.

Basically, I have a boolean called “canMove” that is set to true at the beginning. This is in my playermovement script, In the AIDestinationSetter script, I want to say:

  	if (GameObject.FindWithTag("Player").GetComponent<PlayerMovement>().canMove)					
  		target = GameObject.FindWithTag("Player").transform;
  		target = one of several points I have set up off-screen

But when trying to run the game I get the following error: error CS0246: The type or namespace name ‘PlayerMovement’ could not be found (are you missing a using directive or an assembly reference?).

It is probably worth noting that when I don’t try to use the if statement (and just have target = GameObject.FindWithTag(“Player”).transform; it works. So it’s the getcomponent part that is broken)

I’m a complete beginner when it comes to gamedev- so even if you think your suggestion sounds obvious/silly, PLEASE reply to this with any ideas you think might help me. Thank you!

You have a gameobject called “Player” in your scene. It needs a script with a class (probably derived from MonoBehaviour) called “PlayerMovement” on it and that PlayerMovement class needs a bool field or property called canMove.

Your if statement is finding the ‘Player’ gameobject fine, it then looks for the ‘PlayerMovement’ component on it, but doesn’t find it so it throws the error.

So create a PlayerMovement class with the canMove field, put it on your ‘Player’ object and it will work.

Thanks so much for the reply. I actually have exactly what you’re saying. I’ll attach a screenshot of the two scripts, if that helps.


You should not be modifying the AIDestinationSetter script. Create your own script instead.
The AIDestinationSetter script can only reference other scripts in the pathfinding package, nothing outside it.

Hi, thanks so much for the reply! I completely forgot to get back to you. So i solved this with the following:

foreach (GameObject enemy in enemies) //for each enemy in the array

  	enemy.GetComponent<AIDestinationSetter>().target = spawnPoints[Random.Range(0, >spawnPoints.Length)];//set aipath destination to a random spawnpoint on the edge of the map

Would this be the correct way to change the AIPath destination after the player is defeated? Looks and works great btw