- Home /
Accessing a function within a script assinged in inspector.
Hi,
how can I do something like the following?
var fnToRun: String;
var script: String;
function Start()
{
GetComponent(script).StartCoroutine(fnToRun);
}
Basically, I need to be able to specify the script name and the function in the inspector. However, the snippet above doesn't work and gives me an error saying: 'StartCoroutine' is not a member of 'UnityEngine.Component'.
How do I achieve this? Any help is much appreciated.
Edit:
I need to be able to call non-static functions.
Answer by Martijn Hendriks · Oct 01, 2011 at 02:03 PM
You need to typecast the returned component to a MonoBehaviour. I'm no javascript coder, but in C# it looks like
((MonoBehaviour)this.GetComponent(script)).StartCoroutine(fnToRun);
( code not tested )
Jep that's how it looks like in a single line :D but in UnityScript (JS) you can't use the c-style cast. You have to use the as-cast:
(GetComponent(script) as $$anonymous$$onoBehaviour).StartCoroutine(fnToRun);
But complex-single-line commands doesn't help to read the code ;)
Answer by nasapc123 · Oct 01, 2011 at 01:25 PM
i would use send message myself:
var fnToRun: String;
var script: String;
function Start()
{
var scriptObject = GetComponent(script);
scriptObject.SendMessage(fnToRun);
}
Getting the script component in quite pointless in the case of Send$$anonymous$$essage since Send$$anonymous$$essage will be sent to all scripts on this object.
ok I'll try that thanks. But isn't send message very expensive on iPhone? I'm planning on calling functions through this method a lot.
haha didnt realise it was the same object :D all good,just use Send$$anonymous$$essage(fnToRun);
Answer by Bunny83 · Oct 01, 2011 at 02:12 PM
The problem is that GetComponent returns a Component-reference but only MonoBehaviour components have the StartCoroutine function. You need to cast it to MonoBehaviour to access the function.
var fnToRun: String; var script: String;
function Start() { var scriptObject : MonoBehaviour = GetComponent(script); scriptObject.StartCoroutine(fnToRun); }
However that's in general not a good approach. You should work with direct references and not with string-based reflection-like calls.
If you really need the function to be dynamically called, at least use a reference to the script:
var fnToRun: String; var script: MonoBehaviour; // Drag the script-component onto the variable.
function Start() { script.StartCoroutine(fnToRun); }
Or when the script is located on the same object it's probably easier to use SendMessage:
var fnToRun: String;
function Start() { SendMessage(fnToRun); }
edit
Since it seems a lot of people are not familiar with the Unity editor here is a tutorial how to add another inspector and how to assign a component to a variable:
You can also drag the GameObject, but Unity will take the first Component that matches the required type. If you have only one script attached you can just drag the GameObject (Same happens with a transform component when dragging an GameObject onto a variable)
This is actually also the case with the method. In C# you can use delegates, but I have no idea how this is done in javascript.
I tried this approach but I can't drag the script onto the script variable in inspector. I can only drag a gameobject. What if my gameobject has more scripts assigned. Thanks.
Just for basic understanding! Why do you want to have the inspector have this information about the script and its function? Because you already know which method to call from which script in code, right? If you want to dynamically choose which method to call from a script, maybe a simpler solution would be to make a switch.
The purpose of this is a multi-use touch button script. Let's say I have a guitexture and I assing this button script. This script lets me enter the script name and function name to execute. Therefore I can use the same button sctipt to run any function in any script. I have tried switch but it's unclear and always requires updating as I add more functions and scripts.
I hope this helps. Oh, and thanks for helping.
Did you try the solution of $$anonymous$$e, or was this also malfunctioning? Since it is impossible to link a $$anonymous$$onoBehaviour to an inspector variable, I think the best way to go is the string based search.
Your answer
![](https://koobas.hobune.stream/wayback/20220612232524im_/https://answers.unity.com/themes/thub/images/avi.jpg)