- Home /
Yield WaitForSeconds never finishes?
I'm having trouble with this section of code. The code words up to Debug.Log("Power Up") and then doesn't go any further and I don't get Debug.Log("POWER DOWN")
function OnCollisionExit(collision : Collision){
if (collision.collider.tag == "player") {
Destroy (gameObject);
Player.speed = 40;
Debug.Log("POWER UP");
yield WaitForSeconds (5);
Player.speed = 25;
Debug.Log("POWER DOWN");
}
}
Answer by whydoidoit · Apr 28, 2013 at 06:20 PM
You've destroyed the GameObject at the beginning. Therefore the coroutine will never execute after it yields and the object is really obliterated.
You probably want to hide it rather than destroy it at the top, then Destroy after the yield.
function OnCollisionExit(collision : Collision){
if (collision.collider.tag == "player") {
if(collider) collider.enabled = false;
if(renderer) renderer.enabled = false;
Player.speed = 40;
Debug.Log("POWER UP");
yield WaitForSeconds (5);
Player.speed = 25;
Debug.Log("POWER DOWN");
Destroy(gameObject);
}
}
Perfect, thanks! For anyone who happens to pass by, I changed Destroy (gameObject); to renderer.enabled = false;
Answer by sfc.itzhak · Apr 28, 2013 at 07:32 PM
Hey ,
when you call the destroy(gameobject) you kill the gameobject with the script the power up is being called cause it waits till the end of the frame to delete the game object
function OnCollisionExit(collision : Collision){
if (collision.collider.tag == "player") {
Player.speed = 40;
Debug.Log("POWER UP");
yield WaitForSeconds (5);
Player.speed = 25;
Debug.Log("POWER DOWN");
Destroy (gameObject); } }
this will work
But probably won't have the desired effect of hiding the pickup - which will continue to be able to be collided with.
Your answer