Coroutine using c# doesn't seem to be running?

I’ve been reading through a lot of posts on coroutines, but nothing jumps out to me as being my problem.

I load a level as a coroutine with a yield after the Application.LoadLevel and then I create the player in the level. I do this to ensure the level is fully loaded before I create my player in the level. Technically I don’t think I have to do this, but this was some code I pulled from a demo and it seems to work right so I left it in.

Now I’m testing some code to jump between different levels and and the call to the coroutine is getting called but when I set a break point I never step into it. I get to it, but then it immediately steps back to the calling routine, and the code doesn’t execute. If I remove the IEnumerator and yield statements then it works fine. I’m trying to understand why it’s not working the way I expect it to so I don’t run into issues in the future.

Also, when I remove the IEnumerator and yield and let it run I see it immediately create my player as expected then the player disappears almost just as fast. The really weird thing is when I instantiate the player I change the camera to the players camera. When I see the player instantiate it flips for a second to the players camera which also disables the level camera, but when the player disapears it goes right back to the level’s camera. It’s weird. I’m sure its something stupid I’m doing but I can’t put my finger on it.

If anyone has any ideas or suggestions I’d appreciate it.

Here’s my coroutine:

Thanks

You should try

yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();

Works for me.

It should be “yield return null” ideally. It’s functionally the same as “yield return 0”, but more efficient. You should avoid allocating new objects unless necessary.

–Eric

thanks but none of these are making a difference. I’ve tried both just for good measure, and the code works fine in a different part of the app, loading the first level, but now using it again (cut/paste) for testing its not working. I set a break point at LoadLevel2 and I hit the break point, but I’m unable to step into it, maybe this is expected for a coroutine? However the code doesn’t seem to run either. I place a Debug.log as the first line of the coroutine and get nothing in the log. If I remove the IEnumerator (what makes it a coroutine in c# and remove the yeilds, it works fine). My concern is it should work either way and I want to understand why it isn’t so I don’t make the same mistake again.

A second issue with the same routine is that after it creates the player (at the end) a frame later the player disappears (blinks on just for a moment) and I don;t know why. I set a break at every destroy call in the code and don’t seem to be hitting any of them. Maybe the two issues are related and maybe they aren’t, I’ll keep working on it, but I’m almost at my wits end.

Thanks

Are you starting the coroutine?

Like-

/
/
StartCoroutine(LoadLevel2);
/
/
1 Like

Is this Coroutine being started from within another Coroutine?

As far as I know that never really works right. I have a state machine that uses coroutines for each state, then I once tried to start a separate routine from within one of those state routines and it just seemed to crap itself, even using the debugger and stepping though it line by line I couldn’t figure it out.

Thanks All_American, using StartCoroutine fixed both issues. Always something new to learn :slight_smile:

Sweet, I’m glad I could help…:slight_smile: