- Home /
Referencing functions from other scripts as variables
I would like to use an editor script to initialize a list of buttons that will perform various actions on objects in my scene. I am wondering how to assign functions to these buttons as varialbes if the functions are defined outside the editor script.
More specifically:
I have a GUI controller script (SomeNeatScript) that performs actions on GameObjects based on GUI events. I would like this script to contain an array of buttons that will each call a different function when clicked. I would like to assign functions to the buttons in an initialization routine that will be called from an Editor Script. I would like the assigned functions to be defined in the GUI script.
Some code:
/* from some editor script */
static function initializeButtons () { var myScript = Selection.activeTransform.GetComponent(SomeNeatScript);
var buttonNames = new Array(
"buttonA",
"buttonB",
"buttonC",
"buttonD"
);
var buttonFunctions = new Array(
myScript.functionA,
myScript.functionB,
myScript.functionC,
myScript.functionD
);
var someButtons = new Array();
for ( var i = 0; i < buttonNames.length; i ++ ) {
var freshButton = new ActionButton(buttonNames[i], buttonFunctions[i]);
someButtons.Push(freshButton);
}
myScript.buttonList = someButtons.ToBuiltin(ActionButton);
}
/ ActionButton definition /
class ActionButton { var name : String; var actionFunction : Function;
function ActionButton ( myName : String, myFunc : Function )
{
name = myName;
actionFunction = myFunc;
}
}
/ from SomeNeatScript /
var buttonList : ActionButton [];
function OnGUI () { for ( var currentButton : ActionButton in buttonList ) { if ( GUI.Button(somePosition, currentButton.name) ) { currentButton.actionFunction(); } } }
function functionA () { Debug.Log("I am function a"); }
function functionB () { Debug.Log("I am function b"); }
. . .
Ideally I would like to reference the names of the assigned functions dynamically (by building Strings based on the button's name) but would settle for the approach outlined above.
Any help would be greatly appreciated...
Edit
As a workaround I can assign the functions in the Start function of my GUI script, but I'd still like to know if it can be done from an editor script.
And I'd REALLY like to know how to dynamically reference functions by name so I could skip this whole process...
Answer by marceloroca · Apr 13, 2011 at 09:57 PM
Try to use Component.SendMessage. Or, you can use reflection, but is more complicated.
Answer by Statement · Apr 13, 2011 at 09:58 PM
- You can make use of SendMessage.
- You could make use of a table of names and look methods up by name in a prebuilt dictionary.
- You could make use of reflection to look up methods by name of an object, to later invoke.
- You could create your own editor/inspector to ease this process.
A very basic example of reflection to call a method on an unknown object by name:
function Call(var obj : System.Object , var message : String) {
obj.GetType().GetMethod(message).Invoke(obj, null);
}
This search for a public method named message
on the obj
, and calls it without parameters. Any failure will throw an exception.
Example usage:
var obj = Selection.activeTransform.GetComponent(SomeNeatScript);
Call(obj, "buttonA"); // Attempts to call 'function buttonA()'
Reflection is the most powerful way of getting the methods you want to use. Reflection means for example that you discover members of types, like a method. Then you can invoke that method, given an object and optional parameters.
Reflection can also be used to present the user with a set of functions that could be called, yielding a richer editor.
Answer by jahroy · Apr 13, 2011 at 10:06 PM
Awesome. Looks like SendMessage will work quite well. Thanks very much!
Unity rules!
Just know that all scripts on the same object will get the call (if you have same function names on several scripts), and you can only use that on game objects and components. As long this is no problem I'd also choose this solution.
Answer by yester30 · Mar 28 at 01:38 PM
I'm here long after the war, but what about
public MonoBehaviour MonoBehaviourToCall; // the script component you want to call
public string functionName = ""; //name of the desired function to call there
fill those 2 refs and then later call it with
MonoBehaviourToCall.Invoke(functionName, 0f);
Your answer

Follow this Question
Related Questions
how to access variables in a function script attached to a clone 1 Answer
problems accessing variables and functions in another script 1 Answer
Can I pass a variable to a function to be assigned in c# 2 Answers
Referncing variables such as OnTriggerEnter(Collider other) thumb rule? 1 Answer
Change a variable via functions 1 Answer