- Home /
How to load scenes randomly without repeating previous scenes?
I have 15 scenes which I want to all load randomly without repeating previous random scene. How would I do it? I have tried using List, Random.Range and RemoveAt but the List does not retain/persist its current values between scenes.
I am a newbie in Unity C#. I have been working for it for too long. Thanks for the help in advance.
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
public class ChangeScene: $$anonymous$$onoBehaviour
{
void ChangeSceneByRandomIndex()
{
int currentscene = Scene$$anonymous$$anager.GetActiveScene().buildIndex;
int rando$$anonymous$$dex=Random.Range(0, Scene$$anonymous$$anager.sceneCount);
if(currentscene !=rando$$anonymous$$dex)
{
Scene$$anonymous$$anager.LoadScene(rando$$anonymous$$dex);
}
}
}
Answer by Nk_Khumbhani · Dec 18, 2019 at 06:38 AM
public class SceneLoad : MonoBehaviour { public List SceneNames; private void Awake() { DontDestroyOnLoad(transform); }
public void LoadRandomScene()
{
if(SceneNames.Count>0)
{
int no = Random.Range(0, SceneNames.Count);
string str = SceneNames[no];
SceneNames.RemoveAt(no);
SceneManager.LoadScene(str);
}
}
}
I add this script in main scene from which I want to call random scenes. Following script add in scene which you want to call randomly in button click event.......
public void BtnClick() { GameObject.FindWithTag("SceneCall").GetComponent().LoadRandomScene(); } ........
And in my case it works fine. I hope it useful for you also
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Drawing a Mandelbrot Set problem 0 Answers
Distribute terrain in zones 3 Answers
Learn C# In General Or Learn It With Unity? 1 Answer
Movement using the Scrollbar 1 Answer