Click Forward and Backward thru series of Scenes.

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.

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneCycle : MonoBehaviour {
     // Use this for initialzation
     private void Start()
     {
        
     }
     // Update is called once per frame
     void Update()
     {
         public void NextScene()
         {
             SceneManager.LoadScene("Blank");
         }
         public void PrevScene()
         {
             SceneManager.LoadScene("Blank");
         }
     }

here you go man, give this a try

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneCycle : MonoBehaviour {
   
    private int currentScene = 0; //used to control with is the current active scene
    public int maxSceneCount;  //used to set the max number us scenes

    private void Start()
    {
        if(maxSceneCount == null) //check if this is set
        {
            Debug.Log("you need to set maxSceneCount via Unity Editor");
        }
    }

    void Update()
    {
    
    }

    public void NextScene() //use this on the button
    {
        if(currentScene == maxSceneCount) //check if we have reached to last scene
        {
            currentScene = -1; //set the scene to -1 so that when we add 1 it will equal 0
        }

        currentScene++; //add 1
        SceneManager.LoadScene(currentScene);  //load the scene
    }

    public void PrevScene() //use this on the button
    {
        if(currentScene == 0)//check if we have reached to first scene
        {
            currentScene = maxSceneCount + 1; //set the scene to to the max +1
        }
        currentScene--; //subtract 1
        SceneManager.LoadScene(currentScene); //load the scene
    }
}

That worked out great. Thanks!

more importantly, do you understand the code?

Yes, your markup\notes really helped. Thanks.

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.

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneCycle : MonoBehaviour {
  
    private int currentScene = 1; //used to control with is the current active scene , never use 0, it's MainMenu
    public int maxSceneCount;  //used to set the max number us scenes, my example is 3 levels
    private void Start()
    {
        if(maxSceneCount == null) //check if this is set
        {
            Debug.Log("you need to set maxSceneCount via Unity Editor");
        }
    }
    void Update()
    {
  
    }
    public void NextScene() //use this on the button
    {
        if(currentScene == maxSceneCount) //check if we have reached to last scene
        {
            currentScene = 1; //set the scene to 1 so that when we add 1 it will equal 1, never use 0, it's MainMenu
        }
        currentScene++; //add 1
        SceneManager.LoadScene(currentScene);  //load the scene
    }
    public void PrevScene() //use this on the button
    {
        if(currentScene == 1)//check if we have reached to first scene
        {
            currentScene = maxSceneCount + 1; //set the scene to to the max +1
        }
        currentScene--; //subtract 1
        SceneManager.LoadScene(currentScene); //load the scene
    }
}

Almost working correctly :slight_smile:

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.

i think I only had 1 mistake

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneCycle : MonoBehaviour {
    private int currentScene = 1; //used to control with is the current active scene , never use 0, it's MainMenu
    public int maxSceneCount;  //used to set the max number us scenes, my example is 3 levels
    private void Start()
    {
        if(maxSceneCount == null) //check if this is set
        {
            Debug.Log("you need to set maxSceneCount via Unity Editor");
        }
    }
    void Update()
    {
    }
    public void NextScene() //use this on the button
    {
        if(currentScene == maxSceneCount) //check if we have reached to last scene
        {
            currentScene = 0;
        }
        currentScene++; //add 1
        SceneManager.LoadScene(currentScene);  //load the scene
    }
    public void PrevScene() //use this on the button
    {
        if(currentScene == 1)//check if we have reached to first scene
        {
            currentScene = maxSceneCount + 1; //set the scene to to the max +1
        }
        currentScene--; //subtract 1
        SceneManager.LoadScene(currentScene); //load the scene
    }
}

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);

good deal, glad to help.

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.

can you tell me more about what is not working?
can you post your current code?

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneCycle : MonoBehaviour
{

    private int currentScene = 1; //used to control with is the current active scene , never use 0, it's MainMenu
    public int maxSceneCount;  //used to set the max number us scenes, my example is 3 levels
    private void Start()
    {
        if (maxSceneCount == null) //check if this is set
        {
            Debug.Log("you need to set maxSceneCount via Unity Editor");
        }
    }
    void Update()
    {

    }
    public void NextScene() //use this on the button
    {
        if (currentScene == maxSceneCount) //check if we have reached to last scene
        {
            currentScene = 1; //set the scene to 1 so that when we add 1 it will equal 1, never use 0, it's MainMenu
        }
        currentScene++; //add 1
        //currentScene = SceneManager.GetActiveScene().buildIndex + 1;// Didn't work because it cycles thru all scenes, not just 1-3.
        SceneManager.LoadScene(currentScene);
    }

    public void PrevScene() //use this on the button
    {
        if (currentScene == 1)//check if we have reached to first scene
        {
            currentScene = maxSceneCount + 1; //set the scene to to the max +1
        }
        currentScene--; //subtract 1
        SceneManager.LoadScene(currentScene); //load the scene
        //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1); // Didn't work because it cycles thru all scenes, not just 1-3.
    }
}

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneCycle : MonoBehaviour
{
    public GameObject UIBtn;
    public GameObject eventSys;
    private int currentScene = 0; //Start at 0, becouse we are in the main menu
    public int maxSceneCount;  //used to set the max number us scenes, my example is 3 levels

    void Awake()
    {
        DontDestroyOnLoad(gameObject); //keep this script running in every scene
        DontDestroyOnLoad(UIBtn); //keep this UI buttons in every scene
        DontDestroyOnLoad(eventSys); //keep this EventSystem in every scene
    }

    private void Start()
    {
        if (maxSceneCount < 1) //check if this is set
        {
            Debug.Log("you need to set maxSceneCount via Unity Editor");
        }
    }
    void Update()
    {

    }

    public void NextScene() //use this on the button
    {
        Debug.Log("Next : " + currentScene);
        if (currentScene == maxSceneCount) //check if we have reached to last scene
        {
            Debug.Log("At the LAST Level go back to the beginning");
            currentScene = 0;
        }
        currentScene++; //add 1
        SceneManager.LoadScene(currentScene);
    }

    public void PrevScene() //use this on the button
    {
        Debug.Log("Prev : " + currentScene);
        if (currentScene == 1)//check if we have reached to first scene
        {
            Debug.Log("At the FIRST Level jump to the end");
            currentScene = maxSceneCount + 1; //set the scene to to the max +1
        }
        currentScene--; //subtract 1
        SceneManager.LoadScene(currentScene); //load the scene
    }
}

3198035--244376--BuildsettingsCapture.JPG

a screen capture of the test project

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.