# How can a single script attached to multiple enemies provide separate behavior for each enemy?

I am making a TD game and now stucked at multiple enemies using same script. All is well with scripts attached with one enemy only. Problem is in running multiple enemies.

Here is the overview. I have a Enemy object with which I have attached a 'RunEnemy' script. Flow of this script is:

RunEnemy.cs:

PathF p;
p = gameObject.GetComponent<PathF>();     //PathF is a pathfinding algo which has     'search; function which returns a array of path from starting position
PathList = p.search(starting position);
//-------------------------------

if(PathList != null) {                               //if a way found!
if(moving forward)
transform.Translate(someXvalue,0,0);  //translates on every frame until next grid point

else if(moving back)
transform.Translate(0,someXvalue,0);

...and so on..

if(reached on next point)
PathList = p.search(from this point)   //call again from next grid point so that if user placed a tower enemy will run again on the returned path
}


Now I have attached this script and "PathF.cs" to a single enemy which works perfect. I have then made one more enemy object and attached both of these script to it as well, which is not working they both are overlapping movements. I can't understand why, I have attached these scripts on two different gameobjects but still their values change when either enemy changes its value. I don't want to go with a separate script for each enemy because there would be 30 enemies in a scene. How can I fix this problem?

• A recommendation, in case the code is like the one shown: don't poll gameObject.GetComponent<PathF>() each frame. Store an instance reference to that component in Initialize(), because that's an expensive call. – Elideb Dec 1 '11 at 11:59
• Machine learning, Random.Range, Choices based on location, loads of ways to do it! You need some selection process basically, and it's up to you to decide what it is. – Muzz5 Dec 1 '11 at 20:30
• Depends on the script. – Shuvo Sarker Sep 3 '18 at 8:43

I'd say you are executing one instance of the script, but retrieving only one path. Thus, each time the script runs, it executes the same path for every object it is in. Either store enemy-path pairs in a dictionary (or something like that) or use a script per enemy, with the corresponding encapsulation and local references.

A good option is linking the AI script to a global object and have all enemies needing it register for the service and poll it. Each frame the AI could update all the enemies registered. Or enemies could ask whenever interested for the next point in the path. This lets enemies do stuff while on their way and resume without requesting a new path, if the destination did not change.

Did you try putting RunEnemy in a RunEnemy class then writing another script "EnemyPath.cs" that create a new instance of RunEnemy and assign that script to the enemy prefabs?

Hope this helps

How are you putting the enemies in the scene? If they both exist from the start and have the same code the. Of course they're going to do the same thing (barring RNG).

Just instantiate the second enemy later. You have some kind of class responsible for spawning your enemies over time right?

I would guess that the error isn't in the code you've shown us, but in the code you've not shown us, for the PathF script. There's no reason why components on 2 separate objects would share any data unless you've made it so, by storing the data as static, by choosing to reference the same game object, or some other similar method.