- Home /
Unityscript: Accessing functions from other scripts
Say you're looking to do something like this:
TEST_SCRIPT.js attached to empty object: function Start() { StartCoroutine("CoStart"); }
function CoStart() : IEnumerator
{
while (true)
yield CoUpdate();
}
function CoUpdate() : IEnumerator
{
if (Input.GetKeyDown("space")) {
StartCoroutine(TEST_SCRIPT2.MOVEME);
}
}
TEST_SCRIPT2.js attached to another empty object: var MOVEME : GameObject;
function MoveCube() : IEnumerator
{
MOVEME = GameObject.Find("Cube");
MOVEME.transform.position = Vector3 (0,0,10);
}
What's the syntax to actually call the Coroutine, as StartCoroutine(TEST_SCRIPT2.MoveCube); will get the error: Assets/TEST_SCRIPT.js(16,37): BCE0020: An instance of type 'TEST_SCRIPT2' is required to access non static member 'MoveCube'.
Answer by Peter G · Sep 17, 2011 at 08:47 PM
The error tells you everything wrong with your code. Coroutines can only be called on object instances. So you need an instance of TEST_SCRIPT2
to call your function MoveCube(). Something like this would work:
var testObj : TEST_SCRIPT2 = FindObjectOfType(TEST_SCRIPT2);
//and normally you start the coroutine on the object like this.
testObj.StartCoroutine( MoveCube() );
Doesn't seem to work I'm afraid. Could be because testObj comes up as null if you print(testObj); (could this be because the scripts aren't on the same object)?
That means you don't have an instance of your script. Put it on some GameObject.
They are definitely attached to an object. As I said, they're both attached to empty gameobjects. testObj.StartCoroutine( $$anonymous$$OVE$$anonymous$$E() ); doesn't work in either case, I tried referencing it ins$$anonymous$$d as ("$$anonymous$$OVE$$anonymous$$E") which didn't produce an error but because of the problem with testObj I can't verify that it works.
Again, the testObj I don't think works because there is no object of type TEST_SCRIPT2; you are in reality trying to find an object with a component of TEST_SCRIPT2. But I could be off base here.
I would think FindObjectOfType returns null because TEST_SCRIPT2 is not an object by Unity's definition, but a component. I would use the FindObjectOfType() function to search for the game object with the script, then call the GetComponent method of the returned object.
$$anonymous$$eep in $$anonymous$$d that the FindObjectOfType() function would be kind of slow, depending on the scene. You may have to use singleton pattern so you can have access right away to the game object in question.
So theoretically, say I have two separate gameobjects (obj1 and obj2) with scripts TEST_SCRIPT and TEST_SCRIPT2 attached, respectively. I want to call function Run$$anonymous$$e() in TEST_SCRIPT2 from TEST_SCRIPT1, how would I do this? Assu$$anonymous$$g I have obj2 in a gameobject var, yadda yadda.
$$anonymous$$y problem is it seems to me I can't use StartCoroutine() to call a function in a different script because the functions themselves aren't static (aka, it has nothing to do with what's inside of them, because I make no reference to the objects they're attached to).
Your answer
Follow this Question
Related Questions
running code outside of update! 3 Answers
My function isn't ending but is being repeatedly being called from the update function 1 Answer
Coroutine a function within a loop? 1 Answer
JS and C# how to open in different programs. 1 Answer
how to make object stay destroyed after OnTriggerEnter? 1 Answer