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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LoadingScreenNew : MonoBehaviour {
static LoadingScreenNew instance;
public Text loadingText;
public Canvas canvas;
private int loadProgress;
void Awake()
{
if (instance)
{
Destroy(gameObject);
hide();
return;
}
instance = this;
canvas.enabled = false;
DontDestroyOnLoad(this); //make this object persistent between scenes
}
public static void hide()
{
if (!InstanceExists())
{
return;
}
instance.canvas.enabled = false;
}
//function to check if the persistent instance exists
static bool InstanceExists()
{
if (!instance)
{
return false;
}
return true;
}
// Update is called once per frame
public void Show(string levelName) {
StartCoroutine(DisplayLoadingScreen(levelName));
}
IEnumerator DisplayLoadingScreen(string level)
{
canvas.enabled = true;
loadingText.text = "Loading Progress: " + loadProgress + "%";
AsyncOperation async = Application.LoadLevelAdditiveAsync(level);
while (!async.isDone)
{
loadProgress = (int)(async.progress * 100);
loadingText.text = "Loading Progress: " + loadProgress + "%";
yield return null;
}
}
}
Calling loading scene operation / Sending level name
public void loadLevel(string LevelName)
{
load.Show(LevelName);
Application.LoadLevel(LevelName);
}
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.