So I have a gameobject which loads new scenes when necessary. 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 doesn't get destroyed.. After I have done that I don't get the warning that I have lost the object reference BUT it won't enable the canvas in the setNewNameAndLoadScene() function.
What I mean with enable is that the gui won't appear on the screen but it is in the hirarchy. I checked if it is null which it wasn't. The program runs trough the line of code without problem but the canvas doesn't appear on the gamescreen.
2nd EDIT: I just checked (during the run time) what if I put my canvas (which is a child of my gameobject ) in the canvas of the new loaded scene. what happened is that it showed my canvas. but what I ask me why does it show it when I put it into the scene canvas. before I loaded my first scene my canvas wasn't in the scene canvas but still has shown the progress bar in the gamescreen
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("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;
}
}