Need Help - Understanding How Scripts Effect Objects....

I have a “FollowPath” script which tells the object it is attached to how to use a path (folder full of waypoints) in order to move. It works beautifully! I can instantiate prefab enemies and have them follow whatever path is closest to them. Now I want to add the ability for SOME of my instantiated enemies to be able to stop moving and attack something. I figured since each instantiated enemy has one of these “FollowPath” scripts attached that I could tell the script to stop moving the enemy when they come within range of a target. That works… sorta… but it makes ALL the enemies on screen stop moving! I just want the ONE guy who actually came in range of a target to stop!

I ran into a similar problem last night with some gui… I have some NPCs that are clickable and all do the same thing when clicked, so I wrote a single script that says “OnMouseDown”, set “clicked” to true. When I click the NPC, my GUI pops up. Problem is, if I have more than 1 of the NPCs in the scene and click ANY of them, the GUI gets created once for every single NPC on screen and stacks them all on top of each other… I am having a REALLY hard time understanding why it would run the “OnMouseClicked” for ALL the NPCs… Just because they all have the same script attached doesn’t mean I actually “OnMouseClicked” EVERY NPC in the scene does it???

Someone please help me understand this and how to fix it! I need to be able to click ONE NPC and have GUI pop up for JUST that ONE NPC! I also need to tell an enemy “you can’t move because you are attacking” WITHOUT making ALL the enemies stop moving…

I think if I can get my head wrapped around this madness, the big picture is going to come into focus… :face_with_spiral_eyes:

You’re probably using static variables. There is only ever one instance of a static variable in a class, which causes behavior like that.

–Eric

Not sure I explained thoroughly enough so I’ll try to clarify…

  1. Create an empty game object, call it “Enemy”
  2. Add a mesh to the enemy prefab for aesthetic purposes
  3. Write a “FollowPath” script and add it to our “Enemy” prefab
  4. Write a “spawner” script and add it to a new empty game object called “Spawner” and assign it to spawn our “Enemy” from step 1
  5. Run game, the spawner instantiates 10 “Enemy” objects and they follow the path from step 3.
  6. Add ‘target finding’ code to the “FollowPath” script which says to "stop moving when I come within range of a “target” object.

Why would ALL TEN of the instantiated “Enemy” objects stop moving when “Enemy 3” is the one that found a target???

Yes, I have a “canMove” variable and it was set to static because I was going have the targeting/attacking in a separate script from the ‘follow path’ stuff. I did decide to combine those into a single “AI” script…

So if I make a variable static, then all instances of the script are going to share that variable?

PS: I removed ‘static’ status from the variable and it works correctly, my ‘attackers’ are stopping to attack while the rest keep moving. THANKS!

Yes, static data is stored at class level, not at object instance level. Remove the static keyword for variables that are supposed to be stored per object instance and see if it works.

You don’t have to combine scripts, just don’t use static variables.

–Eric