- Home /
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 :(
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)
Answer by Yokimato · Apr 15, 2013 at 08:37 PM
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.
Your answer

Follow this Question
Related Questions
new Thread vs StartCoroutine 2 Answers
Yield position within while loop in coroutine 1 Answer
Yield and WaitForSeconds not stopping at correct points? 1 Answer
Gets stuck on WaitForSeconds 2 Answers
Coroutines and states 1 Answer