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.

Play some games then dude! Its research.

1 Answer

1

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 Mathf.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.

That's pretty useful. Good to know.