how can I do something like the following?
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.
I need to be able to call non-static functions.
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.
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:
Or when the script is located on the same object it's probably easier to use SendMessage:
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)
(* code not tested *)
answered Oct 01 '11 at 02:03 PM
i would use send message myself:
answered Oct 01 '11 at 01:25 PM