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 *.SetActive (false);*
-
}*
-
Time.timeScale = 0;*
-
GameIsEnded = true;*
-
theText.text = "You win!
Continue?";*
-
butts[0].onClick.AddListener (Continue);*
-
mainCanvas.SetActive (true);*
-
}*
-
if (PlayerController.dead == true && GameIsEnded == false) {*
-
for (int i = 0; i < weapons.Length; i++) {*
_ weapons .SetActive (false);_
* }*
* Debug.Log (“You’re dead!”);*
* Time.timeScale = 0;*
* GameIsEnded = true;*
* theText.text = “You’re dead!
Try 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 .SetActive (false);
* }
Time.timeScale = 0;
GameIsEnded = true;
theText.text = “Time’s up!
Try 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 .SetActive (false);
* }
Time.timeScale = 0;
GameIsEnded = true;
theText.text = “You Win!
Continue?”;
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 .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…