Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Loading a scene isn't removing objects from previous scenes.

Discussion in 'Scripting' started by irjason, Dec 5, 2015.

  1. irjason

    irjason

    Joined:
    Sep 15, 2012
    Posts:
    42
    I have been trying to set up a loading screen for my game, and have ran in to a problem that i just can't think why it's happening. When i switch to a new scene using (application.loadlevel) all the objects from the last scene get carried over. I think it might be the way i am trying to make the loadingscenemanger a persistent object. here is the code.

    Loading scene
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class LoadingScreenNew : MonoBehaviour {
    8.  
    9.   static LoadingScreenNew instance;
    10.  
    11.   public Text loadingText;
    12.   public Canvas canvas;
    13.  
    14.   private int loadProgress;
    15.  
    16.  
    17.   void Awake()
    18.   {
    19.  
    20.   if (instance)
    21.   {
    22.   Destroy(gameObject);
    23.   hide();
    24.   return;
    25.   }
    26.   instance = this;
    27.   canvas.enabled = false;
    28.   DontDestroyOnLoad(this);  //make this object persistent between scenes
    29.  
    30.   }
    31.  
    32.   public static void hide()
    33.   {
    34.   if (!InstanceExists())
    35.   {
    36.   return;
    37.   }
    38.   instance.canvas.enabled = false;
    39.   }
    40.   //function to check if the persistent instance exists
    41.   static bool InstanceExists()
    42.   {
    43.   if (!instance)
    44.   {
    45.   return false;
    46.   }
    47.   return true;
    48.  
    49.   }
    50.  
    51.   // Update is called once per frame
    52.   public void Show(string levelName) {
    53.   StartCoroutine(DisplayLoadingScreen(levelName));
    54.   }
    55.  
    56.  
    57.   IEnumerator DisplayLoadingScreen(string level)
    58.   {
    59.   canvas.enabled = true;
    60.  
    61.   loadingText.text = "Loading Progress: " + loadProgress + "%";
    62.  
    63.   AsyncOperation async = Application.LoadLevelAdditiveAsync(level);
    64.  
    65.   while (!async.isDone)
    66.   {
    67.   loadProgress = (int)(async.progress * 100);
    68.   loadingText.text = "Loading Progress: " + loadProgress + "%";
    69.   yield return null;
    70.   }
    71.   }
    72. }
    73.  
    74.  
    Calling loading scene operation / Sending level name
    Code (CSharp):
    1.         public void loadLevel(string LevelName)
    2.         {
    3.             load.Show(LevelName);
    4.             Application.LoadLevel(LevelName);
    5.         }
    can anyone spot what i have done wrong and why the object's from the last scene are being carried over.

    "Never mind fixxed it" -

    But for some reason the progress is always 0% don't know why the text wont update itself.
     
    Last edited: Dec 5, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. (int)(async.progress*100f);
    3.  
    without the f it'll be implicitly casting to an int and making async.progress 0, i think
     
  3. irjason

    irjason

    Joined:
    Sep 15, 2012
    Posts:
    42
    Awh, i don't think it's a casting issue that is the problem shamefully. I think i have to call it from a update or somthing because of it returning.