- Home /
yield WaitForSeconds preventing further actions.
I have a situation where I am using yield WaitForSeconds in a function and about 50% of the time it works fine and 50% it seems to stop the rest of the function after it from working. I use yield WaitForSeconds in a number of other places in my script and it works just fine. It's only in this function that I'm having the issue with it. Is there something about how it works that I'm not understanding? Here is my current code:
function OnTriggerEnter (theCollision : Collider)
{
if(theCollision.gameObject.tag == "homePoint")
{
if(characterHome)
{
Deactivate();
transform.position = homePoint.transform.position;
Debug.Log("Testing");
yield WaitForSeconds(1);
Debug.Log("Done waiting");
homeBox.SendMessage("LoadCharacter");
homeBox.SendMessage("SetCharacter");
characterHome = false;
}
}
}
Thanks in advance for any help.
Answer by KMKxJOEY1 · Sep 15, 2014 at 06:30 PM
Add in a print debug before the yield so you can check that it even gets that far. I have a sneaking suspicion that your yield isn't what's causing your problem.
I have before. Sorry I forgot to add it in the example code, but that's how I know that the yield is where it is stopping. I'll change it in the example for future reference.
I did just notice something odd. When my character touches the homePoint it prints "Testing" twice and "Done waiting" once when it works. But it prints "Testing" and not "Done waiting" at all when it doesn't work. I'm confused as to why this is since the character only touches the homePoint once, but thought it might help figure out why this is happening.
Ah. I just figured out why it's printing it twice. I have a character controller and a sphere collider on my character that are different sizes. So one touches the homePoint then an instant after, the other touches it. Unfortunately, I need both of these on my character for different reasons so I'll need to figure out a way around this.
For the double collider situation you can add both to GameObjects that are children of your RigidBody. This allows the colliders to function with separate names/tags/layers. They will still all play nicely with the RigidBody.
Once that is done you just implement standard name or tag checking, and only fire the function for one of the colliders.
After lots of checking I realized that even though the function is being called twice do to having two colliders on my character, the yield is still the issue. Even if I delete the sphere collider completely to lessen the confusion, the function is stops at the yield. I commented every line of code and even if I set it to "yield WaitForSeconds(0);" or just "yield;" it stops there every time when just the characterController is present.
However simply deleting the yield makes it play through fine (but there is no pause like I need of course). I am truly baffled as to why this is.