system
1
Hi all
I have a prefab called "trafficLight", it has a js script called "setlights" and that script has a function called "SetLight(state)"
I have lots of trafficLight gameobjects in my scene.
How do I access the function SetLight(n) from any other script (or animation event) in my scene so that all traffic lights get updated at the same time?
Cheers
system
2
Worked out the answer just after asking
GameObject.Find("traficLight").GetComponent(setlights).setLight(0);
If you are using C#, you can create a static List() in the setlights class, and add the current object into it. You can then traverse this list.
class setlights {
public static List<setLights>() lights = new List<setLights>();
public onStart() {
lights.Add(this);
}
}
Somewhere else:
foreach (setlights light in setLights.lights) {
//Do something with the lights here
}