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’.
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)?
– stereosoundThat means you don't have an instance of your script. Put it on some GameObject.
– Peter_GThey are definitely attached to an object. As I said, they're both attached to empty gameobjects. testObj.StartCoroutine( MOVEME() ); doesn't work in either case, I tried referencing it instead as ("MOVEME") 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.
– stereosoundI 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. Keep in mind 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.
– ShinAliSo theoretically, say I have two separate gameobjects (obj1 and obj2) with scripts TEST_SCRIPT and TEST_SCRIPT2 attached, respectively. I want to call function RunMe() in TEST_SCRIPT2 from TEST_SCRIPT1, how would I do this? Assuming I have obj2 in a gameobject var, yadda yadda. My 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).
– stereosound