- Home /
Why is my "yield" statement stopping my script?
Hi, I can't seem to figure out what is wrong with my script. I am creating a roller ball game and I want the ball to disappear when it triggers a hit on the wall. Everything works great separately but when I add a line to get rid of the camera follow script, the script stops at my "yield" command. If I remark out the "yield" command, it works fine but there isn't enough time to complete the explosion and sound before it loads the "reset" level. Can someone please take a look at my script and tell me why it stops at "yield"? Thank you so much!!! Tom
#pragma strict
var explosionEnd : Transform;
function Start ()
{
}
function Update ()
{
}
function OnTriggerEnter (myTrigger : Collider)
{
if(myTrigger.gameObject.name == "WallSide")
{
//Create explosion wherer the ball is (not done yet)
Instantiate (explosionEnd);
Debug.Log ("I hit something.");
//Make the RollerBall disapear
Destroy (gameObject.Find("Player"));
//Get rid of the camera script so it stops trying to find the RollerBall
Component.Destroy(Camera.main.gameObject.GetComponent("CameraFollow"));
Debug.Log ("This is the second message.");
//Wait for the explosion and sound to complete
yield WaitForSeconds(2);
//Load the start over menu level
Application.LoadLevel ("Level 7 Reset");
Debug.Log ("This is a third message.");
}
}
No, that's only for C# (except it would be "yield return new WaitForSeconds").
Answer by Eric5h5 · Jan 29, 2014 at 09:22 PM
You're destroying the object, so the script doesn't exist anymore. The only reason it would work without yield is because destroyed objects aren't removed until the end of the frame.
Oh my goodness!!! I was staring at that script all day and kept thinking to myself that it's a simple script. Why can't I figure it out???
I feel silly but it goes to show you that you need to get away from your work once in a while.
Thanks so much, @Eric5h5!!! I really appreciate your help! $$anonymous$$
it goes to show you that you need to get away from your work once in a while.
All too true. Applies to other stuff like writing docs too...I'll write something that seems fine only to discover the next day that it was actually gibberish.