Load a scene randomly out of a list of scenes?

How can I have a scene loaded randomly out of a list of scenes?

Sorry if my question sounds vague…

Yes, just generate a random number and use a variable to store it. Then use the variable name in Application.LoadLevel();

when you go to build all your scenes - they should all be in a list - on he right of each scene in that list is a number -

application.loadLevel(num);

where num is a random number out of the number of scenes should work

Thanks for the reply!

That would work, except I do not want ALL of the scenes to be chosen from randomly, just a few from a list. How would I make a list?

you can put the list in an array and randomly choose one from the array

Ok, I think I have it. Here is the code just in case anyone else needs it:

Application.LoadLevel(Random.Range(0, Application.levelCount));

Put the desired range in the parentheses where 0 and Application.levelCount is. :smile: I am very glad I found it!

Is their a way to set it that it can only load a scene between the build settings of 3 and 4? So that it can’t load the levels after 4 as I have 6 levels in my scene and its opening them up when I only want it to load up scene 3 or 4.

i did something alike for my weaponspawn on enemys, i dont have acces to my source now but its simple:

create a int[ ]

int[ ] validLevelNumbers = new int [5] {1, 2, 4, 8, 4} //levelNumbers which are ok to load, if a value is more often in this list the probability of it being loaded increases

int randomIndex = Random.Range(0, validLevelNumbers.Length); //creates a random index
Application.LoadLevel(validLevelNumbers[randomIndex]); //load the Level

1 Like

That code looks spot on I’ll test it now and let you know how I got on cheers mate.

You can either use an integer or string array. It depends on if you want to load the scene from the name or the id on the list.

// select random zone by name
string[] zones = new string[3] { "zoneA", "zoneB", "zoneC" };
int random = Random.Range(0, 3);
Application.LoadLevel(zones[random]);
2 Likes

Getting 4 errors, heres my code

function OnTriggerEnter (cubeTrigger : Collider)

{

Debug.Log ("OnTriggerEnter : cubeTrigger.tag = " + cubeTrigger.tag); // shows the tag of the trigger

// if tag is door

if (cubeTrigger.tag == “Player”)

{

string[ ] zones = new string[2] { “Test” “TestTwo” }

int random = Random.Range(0, 2);

Application.LoadLevel(zones[random]);

}

}

The errors im getting are: 8,8 semicolon expected
8,10 semicolon expected
8,32 semicolon expected
8,41 expecting: , found ‘,’.

Any ideas what I’m doing wrong? I fixed the code at the end to say Level not Leve in case you think thats whats wrong and it did noting

Add a semicolon to the end of your string array. (Sorry, I missed that in the code that I posted as an example - fixed.) I just wrote out my example, without testing it, from my memory.

Still getting errors mate, this is the code again

function OnTriggerEnter (cubeTrigger : Collider)

{

Debug.Log ("OnTriggerEnter : cubeTrigger.tag = " + cubeTrigger.tag); // shows the tag of the trigger

// if tag is door

if (cubeTrigger.tag == “Player”)

{

string[ ] zones = new string[2] { “Test”, “TestTwo” };

int random = Random.Range(0, 2);

Application.LoadLevel(zones[random]);

}

}

8,7 ; expected insert at the end (thats the string line)
8,9 ; expected insert at the end
8,31; expected insert at the end
8,40 expecting :, found ‘,’

It’s hard for me to see the code, without it having the code tags around it. Can you edit your post and put the code tags around it? (code] (/code] Replace the ( with [

Also, can you post the code before that?

Got it working mate I simply used this code

Application.LoadLevel(Random.Range(2, 3 +1));

Cheers for the help.

Looking again, I think it’s because UnityScript defines variables differently. The code I gave an example with is in C#. That may be your issue all along. If you still want to use the code I provided, try and find out how UnityScript defines arrays.

Good luck!

Thats exactly what it was, I was using Javascript so it was never going to work with C# code, I’m still learning the basics at the moment so I’m sure I will be using that code for a array in the near future.

Yeah, I don’t use UnityScript… so I don’t know a whole lot about it. I am glad you figured it out though. :wink:

Hi, so do I put this code into a gui button such as a play button on the main menu to do this? Also where do I put the numbers of each level? Like in the level mesh itself?

@crackyourspeakers : This thread is about 5 years old.
This is the API to load a level: Unity - Scripting API: SceneManagement.SceneManager.LoadScene
Here are many great resources learning Unity, in general: Learn
It’ll get your comfortable with many things, including buttons (the whole UI, in fact), loading levels, and a ton of other stuff, of course, too.

Hope that helps.

1 Like