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. Dismiss Notice

find the next level if current is the last one load main menu instead

Discussion in 'Scripting' started by raycosantana, Jun 9, 2014.

  1. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Hi

    I'm using List.find to find the next scene and it works fine, my problem is when I want to load the next scene and the current is the last one. Obviously I get a missing reference error.


    Code (CSharp):
    1.   void ClearFunc(int id){
    2.         GUILayout.Box(coins + " coins \n" + tries + " tries", GUILayout.Height(Screen.height / 2.75f));
    3.         if (GUILayout.Button("Next level", GUILayout.Height(Screen.height / 11))){
    4.             ShowClear = false;
    5.             nextLevel = levels.Find(i => i.Clear == false).Name;
    6.             Application.LoadLevel(nextLevel);
    7.             }
    8.         if (GUILayout.Button("Main Menu", GUILayout.Height(Screen.height / 11))){
    9.             ShowClear = false;
    10.             Application.LoadLevel("Main Menu");
    11.         }
    12.     }
    levels.Find(i => i.Clear == false).Name; Finds the next level which isn't cleared yet, I tried using "if nextLevel == null" but it didn't work.

    I would like to either load the Main menu or simply hide the "Next level button".

    I could have hardcoded this but I like to automate everything for the sake of keeping my code DRY.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Application.levelCount tells you how many levels you have. Don't display your GUI if you are on the last level.
     
    raycosantana likes this.
  3. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    Use Debug.Log () to see what it is when there is no next level. Then write a check for that.
     
  4. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Thanks for your response, I actually found a solution using a different approach and was about to tell a moderator to erase this thread since I didn't need help any more, but I do appreciate the help.
     
  5. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Maybe share your solution, so the community gets to learn something?
     
    zDemonhunter99 likes this.
  6. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    EDIT: This version is better, using List.any() now I can do this without having the missing reference exception

    Code (CSharp):
    1. void ClearFunc(int id){
    2.         GUILayout.Box(coins + " coins \n" + tries + " tries", GUILayout.Height(Screen.height / 2.75f));
    3.         if (nextLevel != null){
    4.         if (GUILayout.Button("Next level", GUILayout.Height(Screen.height / 11))){
    5.             ShowClear = false;
    6.            
    7.             Application.LoadLevel(nextLevel);
    8.             }
    9.         }
    10.         if (nextLevel == null){
    11.             GUILayout.Box("This was the last level", GUILayout.Height(Screen.height / 11));
    12.         }
    13.         if (GUILayout.Button("Main Menu", GUILayout.Height(Screen.height / 11))){
    14.             ShowClear = false;
    15.             Application.LoadLevel("Main Menu");
    16.         }
    17.     }
    18.     // Update is called once per frame
    19.     void Update(){
    20.          
    21.     }
    22.     public void ClearLevel(){
    23.         levels.Find(i => i.Name == Application.loadedLevelName).Clear = true;
    24.         ShowClear = true;
    25.         Time.timeScale = 0;
    26.         if (levels.Any(i => !i.Clear)){
    27.             nextLevel = levels.Find(i => !i.Clear).Name;
    28.         }
    29.     }
    Old post
     
    Last edited: Jun 9, 2014
  7. stigler

    stigler

    Joined:
    May 8, 2014
    Posts:
    3
    Ahm, couldn't you just do:
    Code (CSharp):
    1. if(GUI.Button("Next Level")) {
    2.     int next = Application.loadedLevel+1;
    3.     if(next >= Application.levelCount) {
    4.         next = 0;    //or whatever your main menu is
    5.     }
    6.     Application.LoadLevel(next);
    7. }
    ?
     
  8. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    No.