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.
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âŚ
Here ya go, make sure to set the floor int in the inspector
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");
}
}
}
}
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:
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.
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!