Hello,
Im a newbie on coding and i don
t know how to do when i start the game to random a level and when finish it to load next random one till all levels(scenes) are played !
i have random,range already
public void CompleteLevelC()
{
int index1 = Random.Range(2, 11);
SceneManager.LoadScene(index1);
}
This code randomly play scenes and repeat them continuously.
What i want is random them till all scene from 2 to 11 finish without repeat them and then i will load something else. i try with RemoveAt but did not work …maybe i did it wrong.
Any help will be appreciated.
Hey there,
when you use Random.Range(...)
it will give you a number between the first number and the second (so 2 and 11) in your case.
So what you need is a List of all your levels and you randomly choose an entry, load that entry and then delete it.
So have a static List<int> LevelsToPlay = new List<int> {2,3,4,5,6,7,8,9,10,11};
as a variable in your class. The static modifier will ensure that the list is not reinstantiated every time you change a scene. If your current object is tagged as DontdestroyOnLoad
you do not need this.
and change your code to:
public void CompleteLevelC()
{
//choose the index of a level:
int nextLevelIndex = Random.Range(0, LevelsToPlay.Count);
//get the actual sceneIndex by the index of our list:
int nextLevel = LevelsToPlay[nextLevelIndex];
//remove the sceneIndex from the list to make it not appear again:
LevelsToPlay.Remove(nextLevel);
// load the level:
SceneManager.LoadScene(nextLevel);
}
Let me know if anything was unclear.
Thanks for the answer, i don`t have current object as DontdestroyOnLoad.
I need to put it like that? What will be the fast and secure way to do it and how ?
Thanks…
just add
DontdestroyOnLoad (); like this ?