How can a single script attached to multiple enemies provide separate behavior for each enemy?
- by Syed
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?