Search Unity

Click Forward and Backward thru series of Scenes.

Discussion in 'Scripting' started by schwooba, Aug 17, 2017.

  1. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Hi.

    I have several Scenes that I would like to cycle thru with buttons (Previous and Next). I started this script but don't really know how to finish (still a noob). I've done some research and will continue. In the meantime, can someone help me? Thanks.

    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class SceneCycle : MonoBehaviour {
    6.      // Use this for initialzation
    7.      private void Start()
    8.      {
    9.        
    10.      }
    11.      // Update is called once per frame
    12.      void Update()
    13.      {
    14.          public void NextScene()
    15.          {
    16.              SceneManager.LoadScene("Blank");
    17.          }
    18.          public void PrevScene()
    19.          {
    20.              SceneManager.LoadScene("Blank");
    21.          }
    22.      }
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here you go man, give this a try

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class SceneCycle : MonoBehaviour {
    6.    
    7.     private int currentScene = 0; //used to control with is the current active scene
    8.     public int maxSceneCount;  //used to set the max number us scenes
    9.  
    10.     private void Start()
    11.     {
    12.         if(maxSceneCount == null) //check if this is set
    13.         {
    14.             Debug.Log("you need to set maxSceneCount via Unity Editor");
    15.         }
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.    
    21.     }
    22.  
    23.     public void NextScene() //use this on the button
    24.     {
    25.         if(currentScene == maxSceneCount) //check if we have reached to last scene
    26.         {
    27.             currentScene = -1; //set the scene to -1 so that when we add 1 it will equal 0
    28.         }
    29.  
    30.         currentScene++; //add 1
    31.         SceneManager.LoadScene(currentScene);  //load the scene
    32.     }
    33.  
    34.     public void PrevScene() //use this on the button
    35.     {
    36.         if(currentScene == 0)//check if we have reached to first scene
    37.         {
    38.             currentScene = maxSceneCount + 1; //set the scene to to the max +1
    39.         }
    40.         currentScene--; //subtract 1
    41.         SceneManager.LoadScene(currentScene); //load the scene
    42.     }
    43. }
     
  3. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    That worked out great. Thanks!

     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    more importantly, do you understand the code?
     
  5. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Yes, your markup\notes really helped. Thanks.

     
  6. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Hmmm, one more question. The script cycles thru all my scenes loaded into the Unity Build. How can I limit them to just scenes label "Level1, Level2, etc"? I don't want my MainMenu, Garage scene and others to be there. Sorry I didn't catch that earlier.

    Should I put those specified scenes into a separate folder and cycle thru them from there?

    Thanks.

     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    so look at the build setting window. in the box "scene in build" you will see all the scenes. there is a number on the right hand side, 0 though ? how ever many you have. let say 5, and Unity loads scene 0 at start, so you should see MainMenu as 0, then put the rest in this order,
    MainMenu = 0, Level1 = 1, Level2 = 2, Level3 = 3, Garage = 4

    the maxSceneCount needs to be set to 3

    here is the new code, I hope i didn't miss something, i didn't test.
    the script should now cycle through scenes 1,2,3. not loading 0 or 4

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class SceneCycle : MonoBehaviour {
    6.  
    7.     private int currentScene = 1; //used to control with is the current active scene , never use 0, it's MainMenu
    8.     public int maxSceneCount;  //used to set the max number us scenes, my example is 3 levels
    9.     private void Start()
    10.     {
    11.         if(maxSceneCount == null) //check if this is set
    12.         {
    13.             Debug.Log("you need to set maxSceneCount via Unity Editor");
    14.         }
    15.     }
    16.     void Update()
    17.     {
    18.  
    19.     }
    20.     public void NextScene() //use this on the button
    21.     {
    22.         if(currentScene == maxSceneCount) //check if we have reached to last scene
    23.         {
    24.             currentScene = 1; //set the scene to 1 so that when we add 1 it will equal 1, never use 0, it's MainMenu
    25.         }
    26.         currentScene++; //add 1
    27.         SceneManager.LoadScene(currentScene);  //load the scene
    28.     }
    29.     public void PrevScene() //use this on the button
    30.     {
    31.         if(currentScene == 1)//check if we have reached to first scene
    32.         {
    33.             currentScene = maxSceneCount + 1; //set the scene to to the max +1
    34.         }
    35.         currentScene--; //subtract 1
    36.         SceneManager.LoadScene(currentScene); //load the scene
    37.     }
    38. }
     
  8. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Almost working correctly :)

    I'm able to click Next and advance from Level1 to Level2 but then it stops there. If I click Previous, it goes to the last scene "Level3" in your example.
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    i think I only had 1 mistake

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class SceneCycle : MonoBehaviour {
    6.     private int currentScene = 1; //used to control with is the current active scene , never use 0, it's MainMenu
    7.     public int maxSceneCount;  //used to set the max number us scenes, my example is 3 levels
    8.     private void Start()
    9.     {
    10.         if(maxSceneCount == null) //check if this is set
    11.         {
    12.             Debug.Log("you need to set maxSceneCount via Unity Editor");
    13.         }
    14.     }
    15.     void Update()
    16.     {
    17.     }
    18.     public void NextScene() //use this on the button
    19.     {
    20.         if(currentScene == maxSceneCount) //check if we have reached to last scene
    21.         {
    22.             currentScene = 0;
    23.         }
    24.         currentScene++; //add 1
    25.         SceneManager.LoadScene(currentScene);  //load the scene
    26.     }
    27.     public void PrevScene() //use this on the button
    28.     {
    29.         if(currentScene == 1)//check if we have reached to first scene
    30.         {
    31.             currentScene = maxSceneCount + 1; //set the scene to to the max +1
    32.         }
    33.         currentScene--; //subtract 1
    34.         SceneManager.LoadScene(currentScene); //load the scene
    35.     }
    36. }
     
  10. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Thank you Johne5. Here's what eventually worked for me. I appreciate you leading me in the right path...

    Replace:
    SceneManager.LoadScene(currentScene); //load the scene

    With:
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
     
  11. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    good deal, glad to help.
     
  12. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Didn't quite work out...ugh.

    I'm still kind of a noob so please be patient with me. Maybe the best way would be to write a static array for the 3 levels and then cycling thru them that way. Maybe using GetAllScenes? Once all the scenes are loaded, you can go NEXT using...

    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

    and PREVIOUS using...

    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);

    Don't know how to do that. Will do some reading.

    Any other thoughts? Any help would be appreciated. Thanks.
     
  13. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    can you tell me more about what is not working?
    can you post your current code?
     
  14. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Thanks. Just happen to be working on it now.

    I have 3 separate scenes. With this/your code, when I click NEXT, it goes to scene 2 but then when I click NEXT on scene 2, it does NOT go to scene 3. It just stays on scene 2.

    currentScene = SceneManager.GetActiveScene().buildIndex + 1;/
    This works well but as I mentioned, it cycles thru all my scenes, not just 1-3. It goes to OPTIONS, MAINMENU, ETC.

    Appreciate you helping out.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class SceneCycle : MonoBehaviour
    6. {
    7.  
    8.     private int currentScene = 1; //used to control with is the current active scene , never use 0, it's MainMenu
    9.     public int maxSceneCount;  //used to set the max number us scenes, my example is 3 levels
    10.     private void Start()
    11.     {
    12.         if (maxSceneCount == null) //check if this is set
    13.         {
    14.             Debug.Log("you need to set maxSceneCount via Unity Editor");
    15.         }
    16.     }
    17.     void Update()
    18.     {
    19.  
    20.     }
    21.     public void NextScene() //use this on the button
    22.     {
    23.         if (currentScene == maxSceneCount) //check if we have reached to last scene
    24.         {
    25.             currentScene = 1; //set the scene to 1 so that when we add 1 it will equal 1, never use 0, it's MainMenu
    26.         }
    27.         currentScene++; //add 1
    28.         //currentScene = SceneManager.GetActiveScene().buildIndex + 1;// Didn't work because it cycles thru all scenes, not just 1-3.
    29.         SceneManager.LoadScene(currentScene);
    30.     }
    31.  
    32.     public void PrevScene() //use this on the button
    33.     {
    34.         if (currentScene == 1)//check if we have reached to first scene
    35.         {
    36.             currentScene = maxSceneCount + 1; //set the scene to to the max +1
    37.         }
    38.         currentScene--; //subtract 1
    39.         SceneManager.LoadScene(currentScene); //load the scene
    40.         //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1); // Didn't work because it cycles thru all scenes, not just 1-3.
    41.     }
    42. }
    43.  
    44.  
     
  15. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    next get me a screen shot of the build settings. you can find that under File / Build Settings
    is should look something like the attached image.


    I tested script in unity, I think when you switched scene you didn't have an event system, so when you clicked the btn it didn't do anything. I've added three public gameObects and set them to don't destroy. i'm not sure if you'll need to do this. you might have UI btn's in every scene already.... you will need to drag them into the script in the editor.
    So this is the updated scene code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class SceneCycle : MonoBehaviour
    7. {
    8.     public GameObject UIBtn;
    9.     public GameObject eventSys;
    10.     private int currentScene = 0; //Start at 0, becouse we are in the main menu
    11.     public int maxSceneCount;  //used to set the max number us scenes, my example is 3 levels
    12.  
    13.     void Awake()
    14.     {
    15.         DontDestroyOnLoad(gameObject); //keep this script running in every scene
    16.         DontDestroyOnLoad(UIBtn); //keep this UI buttons in every scene
    17.         DontDestroyOnLoad(eventSys); //keep this EventSystem in every scene
    18.     }
    19.  
    20.     private void Start()
    21.     {
    22.         if (maxSceneCount < 1) //check if this is set
    23.         {
    24.             Debug.Log("you need to set maxSceneCount via Unity Editor");
    25.         }
    26.     }
    27.     void Update()
    28.     {
    29.  
    30.     }
    31.  
    32.     public void NextScene() //use this on the button
    33.     {
    34.         Debug.Log("Next : " + currentScene);
    35.         if (currentScene == maxSceneCount) //check if we have reached to last scene
    36.         {
    37.             Debug.Log("At the LAST Level go back to the beginning");
    38.             currentScene = 0;
    39.         }
    40.         currentScene++; //add 1
    41.         SceneManager.LoadScene(currentScene);
    42.     }
    43.  
    44.     public void PrevScene() //use this on the button
    45.     {
    46.         Debug.Log("Prev : " + currentScene);
    47.         if (currentScene == 1)//check if we have reached to first scene
    48.         {
    49.             Debug.Log("At the FIRST Level jump to the end");
    50.             currentScene = maxSceneCount + 1; //set the scene to to the max +1
    51.         }
    52.         currentScene--; //subtract 1
    53.         SceneManager.LoadScene(currentScene); //load the scene
    54.     }
    55. }
     

    Attached Files:

  16. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    a screen capture of the test project
     

    Attached Files:

  17. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Oh my gosh. Thank you so much for sticking with me. Took some time to narrow down what my problem was but finally figured it out. I had multiple canvases that were conflicting with the code and new canvas. I simply created a new canvas instead of trying to apply it to one of my existing ones. Thanks again. You've been a real blessing.