简体   繁体   中英

Unity WaitForSeconds stuck

I tried to create a simple splashscreen. at first this waitforseconds working, but suddenly after a few week this code doesn't want to work. I tried using debug.log but I just get the "before wait" and the "after wait" doesn't appear even after 5 minutes. I am using unity 4.5, why this is happen?

void Start () {
     guiTexture.pixelInset = new Rect (0, 0, Screen.width, Screen.height);
     StartCoroutine (LoadNextScene ());
 }
 IEnumerator LoadNextScene(){
     Debug.Log ("before wait");
     yield return new WaitForSeconds (DelayTime);
     Debug.Log ("after wait");
     if (NextLevel != "") {
         Application .LoadLevel (NextLevel );    
     }
 }

Just to consolidate answer given by @peterept, following can be reasons due to which your coroutine gets stuck on WaitForSeconds :

  1. The time given in WaitForSeconds is too long. Although this is silly reason, but sometimes this happens
  2. The object from which you are calling the coroutine is getting destroyed. You can check that as follow.

     void OnDestroy() { Debug.Log("Destroyed"); }
  3. From any place, StopAllCoroutines() is getting called which is stopping your coroutine.
  4. Lastly, check that anywhere Time.timeScale is getting set to 0. Because when its zero, it stops Time.deltaTime from increasing and as a result WaitForSeconds never reaches your specified value.You can check your current timescale in Edit -> Project settings -> Time check Time Scale parameter in that. Or you check its value by logging before your coroutine is called.

The last one, was also faced by me and @peterept comment helped me in that.

Your code is perfect. That means one of 2 things:

  1. DelayTime is excessively large (debug it out) or, more likely:
  2. The GameObject is destroyed so the CoRoutine nolonger is running.

Try this:

void Start () {
     guiTexture.pixelInset = new Rect (0, 0, Screen.width, Screen.height);
     StartCoroutine (LoadNextScene ());
 }
 IEnumerator LoadNextScene(){
     Debug.Log ("before wait. Pausing for seconds: " + DelayTime);
     yield return new WaitForSeconds (DelayTime);
     Debug.Log ("after wait");
     if (NextLevel != "") {
         Application .LoadLevel (NextLevel );    
     }
 }

void OnDestroy() {
        Debug.Log("Destroyed");
}

If you really just need to Wait for seconds. You can just use Invoke Instead of Co routine.

Until now, I still can't understand why would anyone use Co routine in a simple basis.

Co routine is usually used to let it do its own work on the background while other still run.

Ex: Checking online Data for your game. ETC. Download Assets.

That is the best way to use Co routine.

But Please, when you just want to delay some Call, just use Invoke.

 void Start(){
        Invoke("LoadMyScene",2);
        // LoadMyScene will be called after 2 seconds.
 }


 public void LoadMyScene(){
   Application.LoadLevel(YourLevelScenehere);

}

Update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM