- Home /
Calling a function from same script, on different objects, through Inspector.
Hey guys, I've been trying to do this for a few hours now and can't seem to find a solution online.
As the title says, I want to have a bunch of functions in a single script, that I can place on different gameobjects, and be able to call the function I want for each gameobject, through the Inspector.
I have a bunch of environmental assets that do a variety of different things. I have the functions already written and working, but would like to combine them into a single script, if possible.
Thanks in a advance!
Answer by Berenger · May 06, 2012 at 09:30 PM
First, create an editor script and add a way to execute a function (MenuItem, EditorWindow, ...).
Then, get all the components you want in your scene.
MyScript[] objects = GameObject.FindObjectsOfType( typeof(MyScript) );
Finally, call the function
foreach( MyScript ms in objects ) objects.MyCoolFunction();
Now, if those objects have the same script but you want a different behaviors on some of them, use an enums or a boolean (or whatever you want) to determine what the function should do.
very usefull. I was trying to acces by having an static reference to the script but was not working. Just a comment. You forgot to cast the type. or put a As Yourtype
Your answer
Follow this Question
Related Questions
Delegates in the Inspector 1 Answer
different objects with the same script wouldn't react correctly 1 Answer
Which function is called when i selected something on the Inspector? 0 Answers
change values after all update function of multiple objects of same script is called 1 Answer
Why inpector keeps methods assigned when changed from public to private 2 Answers