- Home /
Random Map Selection
I have created some scenes that each contain different maps. I don't want people to be able to select scenes, but unity to randomly pick a scene to load.
Answer by Ryuuzoji · Dec 11, 2013 at 11:34 PM
Currently on my laptop and don't have unity with me, but you should be able to do this:
Make sure that all your random scenes are after each other in the build. Then the following code should do the trick:
#pragma strict
var lowest :int;
var highest :int;
var number : int;
function randomLevel () {
number = Mathf.Floor(Random.Range(lowest,highest+1));
Application.LoadLevel(number);
}
Just set the lowest and highst to the numbers where your list starts and ends.
Hope it's useful
@Ryuuzoji - you don't need the $$anonymous$$athf.Floor(). Random.Range() with integers, returns an integer. Nice catch on the +1.
@vooj12 - @Ryuuzoji's solutions is the simplest. As an alternate, you can create an array of scene names and then use Random.Range() into the array to pick one:
String st = names[Random.Range(0, names.Length)];
Application.LoadLevel(st);
Ah, thought it returned a float? Haven't used it much though, and just checked the documentation briefly. I should really get Unity on my laptop...
If you call it with ints you get an int.. floats you get a float.
Your answer
Follow this Question
Related Questions
C# array equal to another array minus one entry. 1 Answer
AI random target 2 Answers
Mapping Cube to Sphere - Using 64 planes as a cube "side" 1 Answer
Selecting Random Object and Setting Variable to True 2 Answers
Random level select 1 Answer