Unity keep crashing with StartCoroutine and yield

I have a “Cubez” prefab (in “Resources” folder) with “SomeScript” script attached.

SomeScript:

#pragma strict

function move(){
	yield WaitForSeconds(1.0);
}

In my scene I have an empty GameObject with “GoScript” script attached.

GoScript:

    #pragma strict
    var test : CallSome;
    function Start () {
        test = new CallSome();
        test.instance = Instantiate(Resources.Load("Cubez", Transform),Vector3(0,0,0), Quaternion.identity) as Transform;
    }
     
    function Update () {
        if(test.go()){
            StartCoroutine(test.getAndMove());
        }
    }

Plus I have “CallSome” script with “CallSome” class (used by “GoScript” script).

CallSome

    #pragma strict
     
    class CallSome{
        var instance : Transform;
        var i : int;
       
        function CallSome(){
            i = 0;
        }
       
        function getAndMove(){
            var script : SomeScript = instance.GetComponent(SomeScript);
            yield script.move();
        }
       
        function go() : boolean{
            if(i < 6) i++;
            return i == 5;
        }
    }

My GoScript calls “StartCoroutine(test.getAndMove());” that calls “yield script.move();” and Unity crashes (before doing ANYTHING in move() function, neither an eventual Debug.Log(“some”) in the first line).

Why? Please help me :frowning:

Just a side note, when using UnityScript (as opposed to C#), you do not need to call StartCoroutine on yields. I'm not sure if it's related since I typically stick to C#, but it's a suggestion. ([source][1]) [1]: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html

1 Answer

1

Also…My guess is that it errors out on the instance.GetComponent(SomeScript); part of the getAndMove() function call BEFORE it calls move(). Following your script, if “cubez” isn’t found…instance is null and therefore calling GetComponent would fail.

The biggest reason for the Instantiate(Resources… call would fail is the your “cubez” prefab is not located in a folder called ‘Resources’ which is a requirement for loading prefabs by name.