- Home /
Random levels(scenes) without repeat them till last one is played
Hello, I`m 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.
Answer by Captain_Pineapple · May 11, 2020 at 11:48 AM
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.
I`m bothering you again :) sorry
It works perfectly and thanks for the help...but now i have another question :)
How can i do after all levels(scenes) are played to send him to other stage for example to the main menu ...
mainmenu is level0 in build settings....
well if you completed all levels the list of levels should be empty correct? So you can just check for that and if the list is empty load your main menu.
Thanks again... i manage to do it :) just attache at the end of the CompleteLevelC function this lines
if(LevelsToPlay.Count < 0)
Scene$$anonymous$$anager.LoadScene(leveltoLoad);
If i want to add the previous scene again after deleting it. So it'll be possible to go from map1 > map2 > map1 again, where and how would i do that?
Answer by Jointuletz · May 11, 2020 at 12:01 PM
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 ?
if you add the Line DontDestroyOnLoad(gameObject);
to a script then the gameObject will not be destroyed when changing scenes. If you are not used to using this then i'd suggest not to use it here since you have to take care of some other things like: Assume you have object A as dontdestroyonload and A is part of Scene 1. Now you change to Scene 2 and back to Scene 1. Now you will have 2 isntances of Object A which will both not be destroyed when you change scenes. So in this case using a static List will be the easier solution for you.
Your answer
Follow this Question
Related Questions
problem Insatiate and destroy auto 0 Answers
Unlockable Levels - Object Masking 0 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
The right way to generate levels like Knife Hit or Paint hit game style. 1 Answer
Light culling using OnPreRender and OnPostRender after updating to 4.3 2 Answers