Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Loading Level after 3 seconds not working

Discussion in 'Scripting' started by Bloodember, Jan 28, 2016.

  1. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    I've got a problem where I Invoke a level to load after 3 seconds and it's not working. I've also tried using an IEnumerator and it doesn't work anymore either. Anyone no why? I've used IEnumerator for the past 2 years and now it just doesn't work.

    Here's my two scripts
    Code (csharp):
    1.  
    2. private int num = 3;
    3.  
    4.  
    5.    IEnumerator Wait()
    6.    {
    7.      yield return new WaitForSeconds (num);
    8.      Application.LoadLevel (1);
    9.      }
    10.  
    11.    void Start()
    12.    {
    13.      StartCoroutine (Wait());
    14.    }
    15.  
    Code (csharp):
    1.  
    2. void Start ()
    3.   {
    4.   Invoke("Load", 3);
    5.    }
    6.  
    7.   void Load()
    8.   {
    9.   Application.LoadLevel("LevelOne");
    10.   }
    11.  
    12.  
    Thanks. I'm also on Unity 5.2.1f1
     
  2. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    Does your level not load at all. or is it loading immediately ?

    If it does not load at all, it could be because you are setting Time.timeScale to 0 before loading the level.
     
  3. TroyDavis

    TroyDavis

    Joined:
    Mar 27, 2013
    Posts:
    78
    On your first script where you set num = 3, I would pass it through to the function for instance;
    Code (CSharp):
    1. IEnumerator Wait(int num)
    2.    {
    3.      yield return new WaitForSeconds (num);
    4.      Application.LoadLevel (1);
    5.      }
    6.  
    7.    void Start()
    8.    {
    9.      StartCoroutine (Wait(3));
    10.    }
    You also need to make sure in your build settings you have your level 1 in it.
     
  4. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    Don't have a Time.timeScale in this scene or the next one.

    Tried that, didn't work. also the level that is to be loaded is 1.
     
  5. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    730
    Application.loadlevel is obsolete in unity 5.3. You need to use

    SceneManager.LoadScene

    EDIT: just read you are using 5.2, sorry
     
  6. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    OK, I figured it out. You now have to have Time.timeScale = 1; in the start function to get IEnumerator or the WaitForSeconds to work now.
     
  7. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Personally, I don't have to "Time.timeScale = 1;" when using yield or IEnumerator.
    I'm using Unity 5.2.2. Maybe it's a 5.2.1 bug of some sort?
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  9. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    It could be, I just don't want to update Unity while in the middle of a project.

    Thanks.
     
  10. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why is it you find the link you remember after posting something less useful o_O

    from the horses mouth: http://blogs.unity3d.com/2015/12/01/custom-coroutines/

    :D