Search Unity

Random Scene

Discussion in 'Scripting' started by Nostradamuzzz, Jul 21, 2022.

  1. Nostradamuzzz

    Nostradamuzzz

    Joined:
    Jan 19, 2022
    Posts:
    9
    Hi everyone!

    Is it possible to create an array or list with selected scenes?
    I was wondering because I have been trying to randomize the next scene that will appear to a player (through a UI button) but that scene has to be part of a set list.

    Any suggestions?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    You can put strings (scene names) into a list, which to me is the most straightforward way of organizing scenes.

    If you feel adventurous, look up ways of making a serializable screen reference, so you can drag/drop scenes into the editor inspector window. I've never bothered as it seems to add very little value to my own workflow. Strings are instantly repeatable and easy to reason about and debug.
     
  3. Nostradamuzzz

    Nostradamuzzz

    Joined:
    Jan 19, 2022
    Posts:
    9
    Thanks for the quick reply!
    I found this: https://answers.unity.com/questions/1259567/load-a-random-scene-from-a-list-of-strings.html
    But I'm still confused by how the scenenames are converted into strings and added to the list directly.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    If you're asking for a program way to get all the scene names, you can loop through the scenes using SceneManager. It has the property sceneCount and the method GetSceneAt(), which you can just get the name from the resulting scene object.