• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rizzutp · May 11, 2018 at 09:15 AM · scenescene-loadingscene-switchingscene-change

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:

alt text

When I click on "Yes", instead of loading again the same scene, it appears this:

alt text

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...

photo-2018-05-11-11-05-10.jpg (94.0 kB)
photo-2018-05-11-11-05-14.jpg (187.3 kB)
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rizzutp · May 11, 2018 at 09:40 AM 0
Share

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.

avatar image Harinezumi · May 11, 2018 at 10:23 AM 1
Share

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.

avatar image Rizzutp Harinezumi · May 11, 2018 at 12:20 PM 0
Share

Removing UnloadSceneAsync() doesn't solve the problem. Anyway, I removed it definitely from my code

avatar image RobAnthem · May 11, 2018 at 01:04 PM 1
Share

$$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.

avatar image Rizzutp RobAnthem · May 11, 2018 at 02:09 PM 0
Share

Yeah, there's a Don'tDestroyOnLoad object. How can I handle it?

avatar image Rizzutp RobAnthem · May 11, 2018 at 02:30 PM 0
Share

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

avatar image Rizzutp RobAnthem · May 11, 2018 at 04:01 PM 0
Share

This is what happens, looking at the hierarchy from the editor: alt text

photo-2018-05-11-17-55-23.jpg (109.3 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

101 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnloadSceneAsync() does not seem to work with additive scenes. 1 Answer

Most efficient way to transition between scenes? 1 Answer

Using parameters of activeSceneChanged? 0 Answers

Scene Loading takes over 3 minutes 1 Answer

How Do I Link Different Scenes? 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges