Problem with changing scenes: Stuck in a loop where two scenes keep loading each other
Hi guys! I'm fairly new to Unity, and I'm not sure how to fix this issue:
I have two scenes: TestTrial and TestTrialFailed (TTFailed).
After executing the TestTrial (which reads in 10 numbers and displays them onscreen), the scene TTFailed should (and does) appear. And when I press the button 'V' in the scene TTFailed, I should be able to (but am not) restart the TestTrial from the beginning (have all the 10 numbers appear again). I figured I'd implement that by just loading scene TestTrial again if V was pressed.
If I start on Scene TTFailed and press V, scene TestTrial loads and starts executing from scratch perfectly.
However, when I start from scene TestTrial, execute it, end up at scene TTFailed, and THEN press V, the scene TestTrial appears for a millisecond but it switches back to scene TTFailed!
I've set breakpoints in my scripts and found out that this is the issue:
(To load scenes I created a class called Loader that looks as follows:)
public class Loader : MonoBehaviour
{
public enum Scene {
TestTrial,
TTFailed,
//more scenes
}
public static void Load(Scene sc) {
SceneManager.LoadScene(sc.ToString()); //fct from UnityEngine.SceneManagement that allows us to load a particular scene
}
}
In the Scene TestTrial (Script TestTrialGame) there are two static variables, bool reached and int count (gets incremented throughout the game). When all the 10 numbers have been displayed, reached is set to true. Scene TTFailed should be loaded once reached is true, so my code in the SceneChange script looks like this:
void Update()
{
if (TestTrialGame.reached)
{
if (TestTrialGame.count >= 5) { //if this is true you want to load scene TTFailed
TestTrialGame.reached = false; //set reached to false bcos static variables don't get reset on scene load?
TestTrialGame.count = 0;
Loader.Load(Loader.Scene.TTFailed);
}
else {
//load another scene
}
}
}
In the scene TTFailed, the code that performs the scene change when V is pressed looks as follows:
void Update()
{
if (Input.GetKeyDown(KeyCode.V)) {
Loader.Load(Loader.Scene.TestTrial);
}
}
While setting breakpoints and debugging, I noticed that after performing Loader.Load(Loader.Scene.TTFailed); at the end of the TestTrial,
SceneManager.LoadScene(sc.ToString()); in the Loader Script is called with 'TTFailed' as argument, TTFailed scene is properly loaded and then when I press V, Loader.Load(Loader.Scene.TestTrial); in the TTFailed script is executed, which once again calls the Loader class code, BUT after getting out of Loader class, we end up back at the statement Loader.Load(Loader.Scene.TTFailed); instead of loading TestTrial completely and executing the scene from the beginning! And this even though 'reached' is set to false!
Can someone help me figure out how to solve this issue? Help would be very very much appreciated :)
Your answer
Follow this Question
Related Questions
Start function not called after reloading scene 0 Answers
multiple of same objects from DontDesroyOnLoad 0 Answers
Multiple active scenes updating but not rendering? 0 Answers
[Unity Beginner] Game design question on scene loading and scene transitioning 0 Answers
changing scene not working? 0 Answers