Canvas will only appear on the game screen when its child of a new loaded scene canvas

So I have a gameobject which loads new scenes when nessesarry. The script attached to the game object has a variable which holds the reference to a canvas element(the canvas element has a progressbar). The problem is that after I load a new scene and I want to load a new scene in that loaded scene I lose the reference to the canvas object because I can’t set him to DontDestroyOnLoad.

What I have done is I put the canvas element (see commented line in start function()) as a child of the gameobject which doesnt get destroyed… After I have done that i dont get the warning that I have lost the object reference BUT it wont enable the canvas in the setNewNameAndLoadScene() function.

EDIT: During the runtime i have put my canvas in the new loaded scene canvas it has shown the cav. but the problem is it only works if its in the scene canvas. It does not help if I enable it. So the problem is if I make my cav a child of the new scene canvas. I lose again my reference as it is not longer attached to my gameobject.

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SceneLoaderGameObject : MonoBehaviour {


	private SceneLoader sl;
	public Canvas cav; //loses reference when loading new scene

	void Start () {
		sl = new SceneLoader ();
		cav.GetComponent<Canvas> ().enabled = false;
		DontDestroyOnLoad (this.gameObject);
		DontDestroyOnLoad (this);
		//DontDestroyOnLoad (this.gameObject.transform.GetChild(0).GetComponent<Canvas>()); 
	}

	public void setNewNameAndLoadScene(string name){

		if(name.Length ==0)
			throw new UnityException("Mate length 0");

		if (cav != null) {
			Slider slid = cav.transform.GetChild (1).GetComponent<Slider> ();
			Text tx = cav.transform.GetChild (2).GetComponent<Text> ();
			Button bttn = cav.transform.GetChild (3).GetComponent<Button> ();

			sl.setNewScene (name);
			sl.setSliderAndTextToChange (slid, tx);
			bttn.onClick.AddListener (() => activateNewScene ());
	    	cav.GetComponent<Canvas> ().enabled = true;
			StartCoroutine (sl.LoadAsynchron (name, bttn));

		}

	}

	public void  activateNewScene(){
		sl.AsgetOP().allowSceneActivation=true;

	}
}

Finaly we found the problem:

Canvas is showing but behind of other canvas, fixed when change value of Sort Order of the Canvas to 1 ( Inspector).

DontDestroyOnLoad(cav.transform);

If Canvas have parent:

DontDestroyOnLoad(cav.transform.parent);

:wink: