Trying to load scene based on random number chosen

I am trying to get the game to randomly choose the next scene based on the number chosen. here is the code

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

publicclassFloor1Select:MonoBehaviour{
publicfloat floor;
privatefloat level;
// Use this for initialization
voidStart()
{
if(floor ==1){
level =Random.Range(1f,2f);
if(level ==1){
SceneManager.LoadScene("Big Chal");
}
if(level ==2){
SceneManager.LoadScene("Speed Run");
}
}
if(floor ==2){
}
if(floor ==3){
level =Random.Range(1f,1f);
if(level ==1){
SceneManager.LoadScene("Skull Boss");
}
}
}
}

it comes up with no errors but it doesn’t load any scenes, if someone could tell me where i am going wrong that would be apreciated.

also using this code

  • voidStart()
  • {
  • SceneManager.LoadScene(Random.Range(0,SceneManager.sceneCount ));
  • }
  • }

wouldn’t work as there are several scenes that i do not want to be randomly selected

Your floor variable doesn’t have a value. That’s why your if statements won’t run and give value to a level variable.

advice: try using switch statement in some cases instead of multiple if statements. Makes code more readable - just my opinion :slight_smile:

I see a couple of things:

  • Have you set a floor value in the GUI? You need to do this as your code is written this way.

  • floor and level are floats. They should be ints. Make them ints as when you do Random.Range(1f,2f), this can return 1.234 or 1.56 or 1.99. None of those are 1 or 2 so that isn’t good. You should also likely use Random.Range(1,2). Don’t use floats. they aren’t needed

  • Random.Range(1f, 1f) that’s just 1.0. No need to use random, just do level = 1;

  • Are there scenes created?

  • You should print out each variable and see what is happening. Double check that the Scene Manager has your screens. Check what the value of floor is. Check what the value of level is.

Return back with your findings and I can help you more if you haven’t fixed it already.

1 Like

It’s a public float so I fill that in, in the scene view. Will that not work?

That works. Look at my post as it will help in the next steps of debugging :smile:

I changed the floats to ints like you suggested, and now it is loading the scene, but it only seems to load the first scene, so is will only load “Big Chal”, I even added the Skull boss scene and increased the range to 3 and it is still always “Big Chal”, there are no errors for scenes not in build settings either…

No wait! It just loaded Speed Run, i think it was to do with the fact it was in the editor, i’ll compile it and test it further

Here ya go, make sure to set the floor int in the inspector :slight_smile:
Keep hitting start/stop and you will eventually get different scenes. Remember it’s random.
Editor/Built makes no difference.

PS If you add something like ‘if (floor == 2)’ it won’t work for those options unless you change the floor variable in the GUI or make that also a random int.

public class Floor1Select : MonoBehaviour {
  public int floor;
  private int level;
  // Use this for initialization
  void Start() {
    if (floor == 1) {
      level = Random.Range(1, 4); //First parameter is inclusive, second parameter is exclusive
      if (level == 1) {
        SceneManager.LoadScene("Big Chal");
      }
      else if (level == 2) {
        SceneManager.LoadScene("Speed Run");
      }
      else if (level == 3) {
        SceneManager.LoadScene("Skull Boss");
      }
    }
  }
}

So i have to add 4 into the random range if i want it to choose from 3 scenes. That would make sense as it doesn’t seem to load skull boss! thanks!

Correct. When using Random.Range(int, int), it’s (inclusive, exclusive). As of why they choose that shrugs but a lot of languages do it. It’s similar to substring methods. So remember, Random.Range(1, max number of levels + 1) hahaha

If you are going to keep the code as is currently and not add any other floors that have their own levels, you could do something like:

level = Random.Range(1, SceneManager.sceneCount + 1);
OR
level = Random.Range(1, SceneManager.sceneCountInBuildSettings + 1);

I can’t remember which one you would want but anytime you add more levels and only have one floor, this would make it so you don’t have to remember to change that last number. But since it seems that you want to have multiple floors and each have unique levels this wouldn’t work for you, but atm it does.

Random.Range can be confusing because if you’re dealting with floats (Random.Range(0.1f, 2.0f), both min and max are INCLUSIVE. However, if you’re dealing with int (Random.Range(2,5)), min is INCLUSIVE and max is EXCLUSIVE. Important to remember when dealing with both floats and ints.

Everything about Random.Range: https://docs.unity3d.com/ScriptReference/Random.Range.html

You are correct about multiple floors with unique levels, and thanks for helping me out, I probably would have given up with this while floor idea if you hand’t helped!

No biggie. If you need further help just respond back here or PM.