- Home /
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;
}
}
because I can't set him to DontDestroyOnLoad.
Why not?
Because it was a child of another canvas appearently. I have fixed that as I have put my canvas a child of my gameobject.
DontDestroyOnLoad(cav.transform);
If Canvas have parent:
DontDestroyOnLoad(cav.transform.parent);
;)
unidad2pete Well I get the same result as before. It couldnt keep reference because it was a child . But the result is the same. It won't show my canvas on the game screen.
During the run time I have put the canvas which was not destroyed in the scene canvas of the loaded scene and it showed my canvas. When I don't put my canvas (cav) in the new loaded scene canvas it won't show it on the game screen.
Im not sure why occurs this, put if I put my canvas child of any object, on start Play, canvas turns disabled, but I add,
cav.enabled = true; // next line of DontDestroyOnLoad(cav.transform.parent);
then, I have no problem, and canvas and her reference on script are showing on scene.
Answer by unidad2pete · Aug 21, 2017 at 10:39 AM
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).
Your answer
Follow this Question
Related Questions
How to keep the same gameobject with different transform values in different scenes 1 Answer
Multiple EvenetSystems in Scene - only have 1 after searching though 1 Answer
Don't destroy On Load make object not interactable in new scene 0 Answers
GUI Elements Carry Over To Next Scene 1 Answer
countdown to persist through scenes 2 Answers