Scenes don't repeat themselves after pressing a restart button
Hi, I made random levels that shouldn't repeat themselves after they are loaded. When the player fails and presses the restart button the scenes that were already in use don't repeat themselves again.
private static List<int> remainingScenes = new List<int>() { 1, 2, 3, 4, };
public void Update()
{
if (MainCamera.position.y >= 10.02)
{
startTimer = true;
}
if (startTimer)
{
timer -= 1.0f * Time.deltaTime;
if (timer <= 0.0f && remainingScenes.Count > 0)
{
startTimer = false;
int sceneIndex = Random.Range(0, remainingScenes.Count);
int scene = remainingScenes[sceneIndex];
remainingScenes.RemoveAt(sceneIndex);
SceneManager.LoadScene(scene);
}
}
}
Thank you :)
Your question is not really clear.
When the user presses the Restart button, do you want the current scene to be reloaded or a random scene to be loaded (among the ones that have never been loaded before)
I want all the random scenes (the previously loaded scenes included) to show up again.
Answer by Hellium · Jun 18, 2019 at 02:56 PM
private static List<int> remainingScenes;
private void Awake()
{
if( remainingScenes == null )
RebuildRemainingScenes();
}
public void Update()
{
if (MainCamera.position.y >= 10.02)
{
startTimer = true;
}
if (startTimer)
{
timer -= 1.0f * Time.deltaTime;
if (timer <= 0.0f && remainingScenes.Count > 0)
{
startTimer = false;
LoadRemainingScene();
}
}
}
private void LoadRemainingScene()
{
int sceneIndex = Random.Range(0, remainingScenes.Count);
int scene = remainingScenes[sceneIndex];
remainingScenes.RemoveAt(sceneIndex);
SceneManager.LoadScene(scene);
}
// Call this function when the user restarts the game
public void RebuildRemainingScenes()
{
if( remainingScenes == null )
remainingScenes = new List<int>();
remainingScenes.Clear();
remainingScenes.AddRange( new int[]{ 1, 2, 3, 4, } );
}
First of all, thank you for responding :) But that's not working. I tried this script, and connected the RebuildRemainingScenes to the OnClick command and nothing changed. I'll explain again: When the restart button is pressed the game restarts, but the scenes that were already loaded before the restart don't load again. I try to make them load themselves again after the restart button is pressed.
Thank you again.
I've edited a little bit the answer, I forgot to call a function in Rebuild
Does Debug.Log( remainingScenes.Count );
prints 4
in the LoadRemainingScene
after the reload?
I added remainingScenes.clear to the Rebuild and added the debug. The console doesn't respond at all which means I guess that the amount of scenes after the restart is infinite. Do you have any idea what could be wrong? Because I can't really find a reason why the script doesn't work.