- Home /
Question is off-topic or not relevant
Load end scene after set of randomized scenes
Hello there,
so as of now I got a starting stage, six levels, and the end stage. I randomized the levels and implemented a timer for when to change the scene. Now my problem is that everytime I run through the application and the last scene is loaded, the end stage does not follow. I want to achieve that the end stage is only loaded after the time has passed AND the last level is loaded. This is my code atm:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
public class LoadingAfterTime : MonoBehaviour
{
private static List<int> scenes = new List<int>() { 2, 3, 4, 5, 6 };
[SerializeField]
private float delayBeforeLoading = 10f;
private float timeElapsed;
private void Start()
{
timeElapsed += Time.deltaTime;
if (scenes.Count <= 0)
{
Debug.LogWarning("No remaining scene to load");
enabled = false;
}
if (scenes.Count <= 0 && timeElapsed > delayBeforeLoading)
{
SceneManager.LoadScene("TheEnd");
}
}
private void Update()
{
timeElapsed += Time.deltaTime;
if (timeElapsed > delayBeforeLoading)
{
int index = Random.Range(0, scenes.Count);
int scene = scenes[index];
scenes.RemoveAt(index);
SceneManager.LoadScene(scene);
}
}
}
Because you never reset timeElapsed
. In Update when you meet timeElapsed > delayBeforeLoading condition reset the timer like timeElapsed=0;
to prevent removal of all your scenes by scenes.RemoveAt(index);
this line immediately.
Sorry I guess my description could have been better. So the problem is mainly that is does not load at all, I achieved loading it but then it would only immediately load after the last scene startet ins$$anonymous$$d of at the end. If I use timeElapsed=0;
scenes just wont load at all after the starting scene.