Search Unity

How can I make random scene loader?

Discussion in 'Scripting' started by FranticThumbs, Feb 8, 2016.

  1. FranticThumbs

    FranticThumbs

    Joined:
    Mar 5, 2015
    Posts:
    58
    Hi all
    I've looked around the web and found some old code but since Unity has changed, I wanted to find out the best way to generate random scenes within Unity 5.

    It's a platform game and I wanted to make 20-30 small scenes that could be picked at random as the player progresses.

    Any suggestions on the best practice and code for making this happen please?

    thanks
    G
     
  2. domkia

    domkia

    Joined:
    Aug 19, 2012
    Posts:
    99
    Lets say you have 30 scenes. Call them something like scene1, scene2, scene3 all the way up to scene30 and just call a simple method:

    int randomScene = Random.Range(1, 30); //gives you a random number from 1 to 30
    Application.LoadLevel("scene" + randomScene.toString()); or SceneManager.LoadScene("scene" + randomScene.toString());
     
    FranticThumbs likes this.
  3. FranticThumbs

    FranticThumbs

    Joined:
    Mar 5, 2015
    Posts:
    58
    Many thanks! I will give this a whirl.
     
  4. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Or just do:
    Code (CSharp):
    1. Application.LoadLevel(Random.Range(0, 30));
    This works regardless of the scene names.
     
    FranticThumbs likes this.
  5. megadavido

    megadavido

    Joined:
    Jan 16, 2017
    Posts:
    10
    Now it should be
    Code (CSharp):
    1. SeneManager.LoadScene(Random.Range(0, 03));
     
    RavenOfCode likes this.