Multiple targeting help

Currently I have a script attached to my enemy characters that tries to detect where the player is. it first uses a view cone as a trigger. if the cone is triggered, it checks with a line cast to see if there are any obstacles.

If both checks succeed, the enemy will now follow the player.
Debug.Log("current target is " +;
chasing = true; display.Chasing();      

However, right now. if one enemy see’s the player; ALL the enemies will follow it.

It’s pretty annoying, and I’m not sure how to fix the issue. If anyone has any ideas, please let me know, I’m happy to show any other code I’m using if it helps fix it.

also, as a bonus, what would be the best way to get the enemy to walk around randomly until the player arrives?


Each enemy should have its own script to govern its actions (this makes it ‘think’ independently from its brethren). You can instead assign that script to the new instance using


or just add the ai script to the enemy prefab(more sensible in this case). You may need to rewrite some stuff. Remember, if you need to access that newly instantiated enemy, you can use a ref var in front of the instantiation call

var newEnemy=Instantiate(enemy,,Quaternion.identity);
var aiScript=newEnemy.GetComponent(AIscript);

//aiScript.player=playerVariable; //or something of the sorts