I have the following code in a player object:
function Start ()
{
GUI = GameObject.FindWithTag("GUI").GetComponent(InGameGUI);
}
function OnCollisionEnter(hitInfo : Collision)
{
if(hitInfo.relativeVelocity.magnitude >= 2) //if we hit it too hard, explode!
{
Explode();
}
}
function Explode() //Drop in a random explosion effect, and destroy ship
{
var randomNumber : int = Random.Range(0,shipExplosions.length);
Instantiate(shipExplosions[randomNumber], transform.position, transform.rotation);
Destroy(gameObject);
GUI.Lose();
}
And my GUI.Lose() function looks like this (this is my GUI game object, separate game object from above):
function Lose()
{
print("before yield");
yield WaitForSeconds(3);
print("after yield");
Time.timeScale = 0;
guiMode = "Lose";
}
When the explode function is called, the loose function is called and I see the message “before yield” print out. I wait three seconds, but I never see the “after yield” message.
If I take the yield out, the function works as I would expect minus the waiting for 3 seconds.
This is on Unity 4. This code is directly from a tutorial that I believe was created on Unity 3.5. I assume the code worked in Unity 3.5 because there are no comments on the sites asking why the yield isn’t working.
What stupid thing am I am doing wrong?