About 'Load All Scenes'!

In this video (objectives)…

  1. Create new method to load straight to our first scene.
  2. Hook up all buttons to loading functionality.
  3. Load all scenes.

After watching (learning outcomes)… Load all scenes in our game using buttons.

(Unique Video Reference: 6_UI_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

There is an alternative to creating LoadStartScreen. I found the SceneManager.sceneCountInBuildSettings. we can use this as modulus for calculating the next scene index.

So instead the LoadNextScreen becomes

public void LoadNextScene() {

    int CurrentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    SceneManager.LoadScene((CurrentSceneIndex + 1) % SceneManager.sceneCountInBuildSettings);
}

The assumption being that you are always going to load the next sceen in the index. The % is a modulus so when we get to the last schene index (2) the next scene index becomes (2+1)%3 = 0

5 Likes

Yep, thats a good way to do it. I didn’t want to introduce modulus at this point in the course so I did it in more of a direct manner.

Hey i did something like that (did add another script just for last scene) can i still use it or will it cause issues later on?

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

public class restart : MonoBehaviour {

public void LoadNextScene()
{
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    SceneManager.LoadScene(currentSceneIndex - 2);
}

Your solution is excellent I never thinked about to use the modulus.

My solution to how to load all scenes is this


In the if I check if the index of the scene is not the last. If it’s not then load the next scene else load the first scene.

But the solution from @Paul_Simmons is the best solution because it can be written with a single line of code. Congrats to you :clap:

3 Likes

Yes it will cause an issue with the first scene. The problem here is when you are at the first scene, which has the index of 0, when you try to load the next scene you do
0 - 2 = -2.

The index of the scenes starts from 0 and there aren’t any negative indexes values in the Build settings.

Modulus is amazing … it is useful in so many circumstances. I mainly use it for partitioning, e.g. I want 3 buckets and things to be distributed in them randomly becomes

bucket = random % numBuckets;
2 Likes

I feel silly now. I thought I was doing pretty good to figure out I needed another separate method to load the start scene. I’m sure it’s just like everyone else’s that went with basics. I’ve included the code for reference.

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

public class SceneLoader : MonoBehaviour {
    public void LoadNextScene()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex+1);
    }
    public void LoadStartScene()
    {
        SceneManager.LoadScene(0);        
    }
}

Your solution works fine until you’re at the last scene and want to load t next scene which it doesn’t exist.

The solution is ok because if the last scene doesn’t have anything to load the next scene that’s ok.

1 Like

Just adding my 2 cents:
SceneManager.LoadScene(++currentSceneIndex % SceneManager.sceneCountInBuildSettings);

1 Like

After I got it working, I asked my wife to play the game and she was well - disappointed. She expected more magic.
At first I laughed thinking well obviously computers aren’t magical. Then I thought, maybe my graphics are off, creating too high expectations…
Then, why not, it would be cool (but much more work) to only guess once and have the guess be right, like real mind reading. I wondered what kind of questions might the amazing mind reading machine ask as it received fuzzy information from the ethers, each question (magically) reducing the set of possible right guesses until only one right guess remains? Done right, maybe the one right guess could seem at least somewhat amazing, making the game - better.
A first question/inquiry might be:
Firstly, I get a sense of the weight of a number, big numbers being heavy, small numbers being lighter. A big number might be considered a number over 500. I’m sensing the number in your mind is a number heavier than 500. Am I right? Y/N
For instance, a second question/inquiry might look something like this:
When I receive information from the ethers, I receive it all in horizontal and vertical line patterns, some vertical lines being short and some long. I’m not receiving any long vertical lines, which makes me think there are no 1’s, 4’s, 6’s, 7’s, 9’s or 0’s in the number you are thinking of. Am I right? Y/N
You get the idea, the set of possible answers keeps getting reduced with no actual guesses, just mystic like questions, until there is only one possible answer, and that answer is the guess made by the amazing machine. A human would have a difficult time quickly coming up with the right number based on the answers to questions like these but an amazing mind reading machine could find it easy (for the machine, maybe not so much for the programmer).
Anyway, just some of my thoughts about how this game became an example for me, of how a game designer might think in terms of improving a game based on user criticism.

1 Like

Hi there,

Can you check this line. It didnt work for me. Did i do something wrong? ++ doesnt work in this case??

I used “++” instead of “+1”

public void LoadNextScene()
{
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    SceneManager.LoadScene(currentSceneIndex++);
}

Try ++currentSceneIndex instead, that way you increase before passing the actual value to LoadScene(…), not after.

thanx Vizibal…

My solution is:

 public void LoadNextScene()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        if(currentSceneIndex + 1 == SceneManager.sceneCountInBuildSettings)
        {
            currentSceneIndex = -1;
        }
        SceneManager.LoadScene(currentSceneIndex + 1);
    }
1 Like

I was coming here to post exactly this, but man your code was elegant! Mine had a conditional lol

1 Like

More compact for sure, and a nice enhancement. It has been a while since I have written any C so I have forgotten these little nuances.

Privacy & Terms