-2
\$\begingroup\$

Is there any way that I can randomly change scenes in Unity C# using arrays?

\$\endgroup\$
  • \$\begingroup\$ it so easy as I can understand your question, could you please add some details what you actually want \$\endgroup\$ – Hamza Hasan Dec 29 '15 at 11:20
3
\$\begingroup\$

No need for an array, you just need to know total amount of scenes you have:

//using UnityEngine.SceneManagement;

void LoadRandomScene(int totalSceneAmount)
{
    int random = Random.Range(0, totalSceneAmount-1);
    SceneManager.LoadScene(random, LoadSceneMode.Single);
}

Note: You need to add all of your scenes to build in Build Settings window.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

If you really want only that then you can do it by this way.

string[] scenes = new string[3]{"Gameplay1","Gameplay2","Gameplay3"};

void LoadRandomScene()
{
    string randomSceneName = scenes[Random.Range(0,scenes.Lenght)];
    Application.LoadLevel(randomSceneName);
}
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Please note that Application.LoadLevel is obsolete now. All Scene loading jobs should be done via UnityEngine.SceneManagement class. \$\endgroup\$ – S. Tarık Çetin Dec 29 '15 at 20:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.