- Home /
How to call a function on a prefab script
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
Answer by ckfinite · Oct 14, 2010 at 01:46 PM
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
}
Answer by Geoff Coope · Sep 29, 2010 at 11:58 AM
Worked out the answer just after asking
GameObject.Find("traficLight").GetComponent(setlights).setLight(0);
Actually this only returns one instance, ins$$anonymous$$d I had to create an Array of object types and traverse it to update all the instances in the scene.
Your answer
Follow this Question
Related Questions
rot not working for 2nd prefab 2 Answers
Calling non applied scripts 3 Answers
What should a script contain? 1 Answer