- Home /
Re-loading a scene but on the background older scenes are displayed
Hello there. I was testing my game, when I noticed a strange thing: when I do game over by making the time expires, loading again the same scene displays on the background the older scenes that I completed precedently.
Here's an example: this pitcure represents the beginning of the second level. Notice the timer, in the bottom right corner. I let the time expires. Then, as it is expected, it appears this canvas:
When I click on "Yes", instead of loading again the same scene, it appears this:
Which is the level completed previously!!! Clicking "yes" another time makes the scene to be loaded again.
I want to avoid this strange behavior, making the scene to be loaded again without display any older scene! This is the code which I use to manage the canvas and the buttons:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MenuScript : MonoBehaviour {
public bool GameIsEnded = false;
public GameObject mainCanvas;
public GameObject[] weapons;
private Text theText = null;
public Button [] butts;
private Scene scene;
private int sceneIndex;
void Start () {
scene = SceneManager.GetActiveScene ();
SceneManager.SetActiveScene (scene);
sceneIndex = scene.buildIndex;
theText = mainCanvas.GetComponentInChildren<Text> ();
butts = mainCanvas.GetComponentsInChildren<Button> ();
Debug.Log (SceneManager.sceneCountInBuildSettings);
weapons = GameObject.FindGameObjectsWithTag ("WeaponSpawn");
}
//Update is called once per frame
void Update () {
// Handling menu for ENEMY mode
if (Maze.genState == "Enemy" | Maze.genState == "Traps" | Maze.genState == "EnemyTraps") {
if (CheckBehavior.verify == true && GameIsEnded == false) {
for (int i = 0; i < weapons.Length; i++) {
weapons [i].SetActive (false);
}
Time.timeScale = 0;
GameIsEnded = true;
theText.text = "You win!\nContinue?";
butts[0].onClick.AddListener (Continue);
mainCanvas.SetActive (true);
}
if (PlayerController.dead == true && GameIsEnded == false) {
for (int i = 0; i < weapons.Length; i++) {
weapons [i].SetActive (false);
}
Debug.Log ("You're dead!");
Time.timeScale = 0;
GameIsEnded = true;
theText.text = "You're dead!\nTry again?";
butts[0].onClick.AddListener (Play);
mainCanvas.SetActive (true);
}
}
if (Maze.genState == "Timer") {
if(CheckBehavior.verify == false && Timer.timeLeft <= 0 && GameIsEnded == false){
for (int i = 0; i < weapons.Length; i++) {
weapons [i].SetActive (false);
}
Time.timeScale = 0;
GameIsEnded = true;
theText.text = "Time's up!\nTry Again?";
butts[0].onClick.AddListener (Play);
mainCanvas.SetActive(true);
}
if(CheckBehavior.verify == true && Timer.timeLeft > 0 && GameIsEnded == false){
for (int i = 0; i < weapons.Length; i++) {
weapons [i].SetActive (false);
}
Time.timeScale = 0;
GameIsEnded = true;
theText.text = "You Win!\nContinue?";
butts[0].onClick.AddListener (Continue);
mainCanvas.SetActive(true);
}
}
}
public void Play () {
mainCanvas.SetActive(false);
Time.timeScale = 1f;
GameIsEnded = false;
CheckBehavior.verify = false;
PlayerController.dead = false;
SceneManager.UnloadSceneAsync (sceneIndex);
SceneManager.LoadScene(sceneIndex, LoadSceneMode.Single);
Debug.Log ("Play pressed!");
}
public void Continue () {
if (scene.buildIndex != SceneManager.sceneCountInBuildSettings) {
mainCanvas.SetActive (false);
Time.timeScale = 1f;
GameIsEnded = false;
CheckBehavior.verify = false;
SceneManager.UnloadSceneAsync (sceneIndex);
SceneManager.LoadScene (sceneIndex + 1, LoadSceneMode.Single);
Debug.Log ("Continue!");
} else {
theText.text = "Demo ended!";
for (int i = 0; i < butts.Length; i++) {
butts [i].gameObject.SetActive (false);
}
}
}
public void QuitGame() {
Debug.Log ("Quitting game...");
Application.Quit ();
}
}
EDIT: I discovered that the maze displayed on the background... it's the newly reloaded scene! All my levels are procedurally generated: it seems that SceneManager.LoadScene doesn't load in time respect to the maze generation method, which "arrives" first.
EDIT: Ok, it's clear that when a game over is performed, the script I wrote enter 2 times in the "Timer" condition, adding two times the SceneLoad method to the button Yes...
EDIT: I discovered that the maze displayed on the background... it's the newly reloaded scene! All my levels are procedurally generated: it seems that Scene$$anonymous$$anager.LoadScene doesn't load in time respect to the maze generation method, which "arrives" first.
I think what happens is that you don't wait for UnloadSceneAsync()
to finish, and LoadScene()
interrupts it. I can't say for sure, but in my experience you can only have one scene load/unload operation active at a time, and you always have to have at least one scene active. Ins$$anonymous$$d you could first load the scene, then unload the previous scene.
But even better, as you are using LoadScene$$anonymous$$ode.Single
, you actually don't even need to unload the previous scene, it will automatically unload every other scene (for example if you used additive scene loading). At least that's how it used to work.
So just try removing the UnloadSceneAsync()
calls, and see if that solves the problem.
Removing UnloadSceneAsync() doesn't solve the problem. Anyway, I removed it definitely from my code
$$anonymous$$aybe your maze object is using DontDestroyOnLoad? which would cause it to persist into the next scene. Never had a problem with a scene loading, though usually I use loading screens and progress bars.
Yeah, there's a Don'tDestroyOnLoad object. How can I handle it?
However, it is not attached to the $$anonymous$$aze Generator. The DontDestroyOnLoad object is attached to a Game $$anonymous$$anager object, keeping track of Player ammos and lives
This is what happens, looking at the hierarchy from the editor:
Answer by Rizzutp · May 11, 2018 at 05:15 PM
Removing the change of the boolean value GameIsEnded from Play() and Continue() solved the problem! Thanks everyone for the hints
Your answer
Follow this Question
Related Questions
Scene Loading takes over 3 minutes 1 Answer
Using parameters of activeSceneChanged? 0 Answers
How Do I Link Different Scenes? 4 Answers
Scene Loading Issue. 0 Answers
Unity Scene loading problem (slow loading until it freezes) 0 Answers