Retaining UI between LoadLevelAsync

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… :wink:

Does anyone know of a tutorial to create ui elements in code. I think if I create them rather than instantiate them they will be retained between scene loads with ‘dontdestroyonload’

There should be no difference between Instantiate and creation from code.
DontDestroyOnLoad should prevent destruction of the object regardless, so the code above on it’s own looks fine.

Are they actually gone from the hierarchy after the load?

One thing to try is to add a Debug.Log(); to the OnDisable on the progress bar and see from where it is called, maybe there is some other script code which destroys them?

Yes. As soon as the async load completes and the new scene loads, the prefab has been destroyed and has gone from the hierarchy. The code-generated canvas is all that remains. There is no code attached to the progress bar and the only thing that accesses it is the script that updates its value and text. Also, the two references created in awake become null.

The testing I did last night appears to uniformly show that code created gameobjects are retained where as instantiated gameobjects are not.

Can you explain better how to debug their destruction? In the bath atm and will have another fiddle later today.

Ps. Rubber ducky say hi! :wink: