This is perhaps more a Unity general question than a beta one. I only place it here due to the UI reference.
If I can explain what I am trying to do?
I decided it would be nice to have a progress bar between loading scenes (levels - shouldn’t it really be LoadSceneAsyn?). The sequence of the scene change code is thus,
Fade out existing scene to black using a canvas with an image and an alpha
Display a progress bar (Instantiated prefab)
Start the LoadLevelAsync function and set the progress bar based on its progress
When scene is loaded, run the scene
Start() in the new scene then fades the canvas to clear
The problems is that I lose the prefab at the moment the new scene has loaded and can find no way to stop it from being destroyed on load? I want to retain it as there is a noticeable delay when a async says it has finished and when the loaded scene becomes active. I want the progress bar to remain at this time.
I initialise the canvas and instantiate the prefab in the Awake function (just the once). This works fine in as much as retaining the canvas. This is the parent of the slider used for the progress bar, but drops it when the scene is loaded.
The init is as - (sorry about untidy code, been fiddling with it for so long it is a little jumbled)
void Awake ()
{
if (!GameObject.Find("SceneFader"))
{
m_active = false;
async = null;
GameObject fadeObj = new GameObject("SceneFader");
fadeObj.AddComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
fadeObj.GetComponent<Canvas>().sortingOrder = 10;
fadeImage = fadeObj.AddComponent<Image>();
fadeImage.color = new Color (0,0,0,0);
progressBar = Instantiate(Resources.Load("Prefabs/UI_PROGRESS_BAR")) as GameObject;
progressBarText = (GameObject.Find("ProgressText")).GetComponentInChildren<Text>();
progressBarValue = (GameObject.Find("Slider")).GetComponentInChildren<Slider>();
progressBar.transform.SetParent(fadeObj.transform);
progressBar.SetActive(false);
DontDestroyOnLoad(progressBar);
DontDestroyOnLoad(fadeObj);
fadeImage.enabled = false;
My finding appears to be that an instantiated prefab cannot be retained between scenes. I tried adding DontDestroyOnLoad(progressBar), but this makes no difference?
I hope there is some super intelligent being on the forum who knows how to retain the prefab, or… A way to construct the slider in code and attach it to the canvas ‘fadeObj’. I tried several ways to do this also, to no avail.
Any help, or even a nudge in the right direction with be very appreciated whilst I still have a few hairs on my noggin…