Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to go to next level

Discussion in '2D' started by Thor-Apps, Apr 21, 2016.

  1. Thor-Apps

    Thor-Apps

    Joined:
    Dec 13, 2013
    Posts:
    32
    Hi,
    I have a simple game with just 1 scene and 1 XML. On the start of the scene I look at the XML where I have the level structure.
    My issues is now how to change to next level, I'll need to pass the number of level to the scene. Also I tried Application.LoadLevel (but it's says is deprecated). How can I do it?

    Thanks
     
  2. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    230
    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    2.  
    3. public class Example
    4. {
    5.     public void LoadLevel()
    6. {
    7.         // load the nextlevel
    8.   SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    9.  
    10.     }
    11. }
    Be sure to add them in File>Build Settings
     
  3. Thor-Apps

    Thor-Apps

    Joined:
    Dec 13, 2013
    Posts:
    32
    Thanks for you reply..
    That will load the next scene in the list, right?
    What if I need the same scene but the level variable increased by 1?
     
  4. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    230
    Yes
    Then load the same scene, but increase the variable and Handle whatever it does on the load, or via a Singleton.
    But thats why scenes are there for, idk why you would want to do this.

    Or perhaps I may be misunderstanding you.
     
  5. Janibasha

    Janibasha

    Joined:
    Mar 7, 2017
    Posts:
    8
    If I am loading all levels in a single scene then
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); will not works.
     
    galanishubham and trramanuj011 like this.
  6. galanishubham

    galanishubham

    Joined:
    May 28, 2019
    Posts:
    1
    Need some changes,
    Use this code :
    private void Start()
    {
    nextLevel = SceneManager.GetActiveScene().buildIndex + 1;
    }
    void OnTriggerEnter2D(Collider2D collision)
    {
    // Application.LoadLevel(loadLevel);
    SceneManager.LoadScene(nextLevel);

    }
     
  7. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    230
    It´s a post from 3 years ago.
    Furthermore, what you posted is the same + an OnTriggerEnter2D that has nothing to do with what OP asked. Perhaps you posted on the wrong thread?
     
  8. hanonymus90Dark

    hanonymus90Dark

    Joined:
    Oct 31, 2019
    Posts:
    1
    thanks for the help i really needed that because i am an beginner :)
     
  9. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    If this didn't work then you didn't go to File>>Build_Settings>>Add_Open_Scenes

    You will need your scenes to be in the proper order inside the build list unless you switch scenes by scene name reference rather than build index.