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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Randomly load a scene?

Discussion in 'Scripting' started by MusicBoxx, Jul 28, 2018.

  1. MusicBoxx

    MusicBoxx

    Joined:
    Jul 17, 2018
    Posts:
    23
    Hello,
    I would like to randomly load a scene. How would I do that?
    My current script is:

    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    2. void Update ()
    3.     {
    4.         SceneManager.LoadScene(Random.Range(1,2));
    5.     }
    I would like it to load a Scene based on the build number, but for some reason it always loads the Scene 1.
     
  2. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    Random.Range int doesnt return the max number, so you'd need to do Random.Range(1,3) for example. It's weird. We know.
     
    MusicBoxx and AcidArrow like this.
  3. MusicBoxx

    MusicBoxx

    Joined:
    Jul 17, 2018
    Posts:
    23
    Thank you so much for helping me out. I tested my script in many different ways by making lists and everything…I should've asked sooner. I feel stupid now, but how could one have known if doing it for the first time, right?
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,025
  5. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    Just so you know, Random.Range(int,int) Never returns the max number. However, Random.Range(float,float) does.
     
    MusicBoxx likes this.