Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Load a scene randomly out of a list of scenes?

Discussion in 'Scripting' started by Teejay5, Jul 22, 2010.

  1. Teejay5

    Teejay5

    Joined:
    Feb 26, 2010
    Posts:
    106
    How can I have a scene loaded randomly out of a list of scenes?

    Sorry if my question sounds vague...
     
  2. Vert

    Vert

    Joined:
    Mar 23, 2010
    Posts:
    1,099
    Yes, just generate a random number and use a variable to store it. Then use the variable name in Application.LoadLevel();
     
  3. waltermana

    waltermana

    Joined:
    Jun 8, 2010
    Posts:
    172
    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 -

    Code (csharp):
    1. application.loadLevel(num);

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

    Teejay5

    Joined:
    Feb 26, 2010
    Posts:
    106
    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?
     
  5. runningbird

    runningbird

    Joined:
    Sep 3, 2009
    Posts:
    382
    you can put the list in an array and randomly choose one from the array
     
  6. Teejay5

    Teejay5

    Joined:
    Feb 26, 2010
    Posts:
    106
    Ok, I think I have it. Here is the code just in case anyone else needs it:

    Code (csharp):
    1. Application.LoadLevel(Random.Range(0, Application.levelCount));
    Put the desired range in the parentheses where 0 and Application.levelCount is. :D I am very glad I found it!
     
  7. Burkie7

    Burkie7

    Joined:
    Apr 25, 2012
    Posts:
    28
    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.
     
  8. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    612
    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
     
    Stubbornnut likes this.
  9. Burkie7

    Burkie7

    Joined:
    Apr 25, 2012
    Posts:
    28
    That code looks spot on I'll test it now and let you know how I got on cheers mate.
     
  10. grim2594

    grim2594

    Joined:
    Nov 5, 2010
    Posts:
    104
    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.

    Code (csharp):
    1. // select random zone by name
    2. string[] zones = new string[3] { "zoneA", "zoneB", "zoneC" };
    3. int random = Random.Range(0, 3);
    4. Application.LoadLevel(zones[random]);
     
    Last edited: May 3, 2012
    Ylex and Justice0Juic3 like this.
  11. Burkie7

    Burkie7

    Joined:
    Apr 25, 2012
    Posts:
    28
    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
     
    Last edited: May 3, 2012
  12. grim2594

    grim2594

    Joined:
    Nov 5, 2010
    Posts:
    104
    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.
     
  13. Burkie7

    Burkie7

    Joined:
    Apr 25, 2012
    Posts:
    28
    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 ','
     
  14. grim2594

    grim2594

    Joined:
    Nov 5, 2010
    Posts:
    104
    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?
     
    Last edited: May 3, 2012
  15. Burkie7

    Burkie7

    Joined:
    Apr 25, 2012
    Posts:
    28
    Got it working mate I simply used this code

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

    Cheers for the help.
     
  16. grim2594

    grim2594

    Joined:
    Nov 5, 2010
    Posts:
    104
    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!
     
  17. Burkie7

    Burkie7

    Joined:
    Apr 25, 2012
    Posts:
    28
    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.
     
  18. grim2594

    grim2594

    Joined:
    Nov 5, 2010
    Posts:
    104
    Yeah, I don't use UnityScript... so I don't know a whole lot about it. I am glad you figured it out though. ;)
     
  19. uneasygamestudios

    uneasygamestudios

    Joined:
    Apr 15, 2015
    Posts:
    6

    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?
     
  20. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Kurt-Dekker likes this.