I want to change a variable in a script which is in multiple gameobjects, I tried to use public GameObject enemy; and in the update I had enemy = GameObject.FindGameObjectsWithTag("Enemy"); but that won't work, how do I select every gameobject with tag "Enemy" and change a variable in all of their scripts?
You're on the right track.
You should probably do it less often than in every Update(), but FindGameObjectsWithTag will work. You then need to get the component (script) that the variable is in, using GetComponent, and then you can set it.
- Gather array of all GameObjects tagged as "Enemy"
- Use a forloop to iterate through all those gameObjects, and use GetComponent (or use Messages) to access the variable inside that script.
You just need to make sure that the variable you're declaring on those objects is publicly accessible (i.e. not in a function, and declared as public)
Instead of using `public` use `static`; that should keep your variable the same value in all instances of the script.