c# - Canvas wont enable it after got loaded in new scene -


so have gameobject loads new scenes when nessesarry. script attached game object has variable holds reference canvas element(the canvas element has progressbar). problem after load new scene , want load new scene in loaded scene lose reference canvas object because can't set him dontdestroyonload.

what have done put canvas element (see commented line in start function()) child of gameobject doesnt destroyed.. after have done dont warning have lost object reference wont enable canvas in setnewnameandloadscene() function.

what mean enable gui wont appear on screen in hirarchy. checked if null wasn't. program runs trough line of code without problem canvas doesnt appear on gamescreen.

2nd edit: checked (during run time) if put canvas (which child of gameobject ) in canvas of new loaded scene. happened showed canvas. ask me why show when put scene canvas. before loaded first scene canvas wasnt in scene canvas still has shown progress bar in 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;   } } 

simply use code below make singletons , public gameobject references of sliders or whatever want activate-deactivate when scene loading. example if have game manager controls components of canvas, make both singleton , reference objects 1 another. source of code here.

public class sceneloadersingleton : monobehaviour {      //static instance of sceneloadersingleton allows accessed other script.     public static sceneloadergameobject instance = null;          //awake called before start functions     void awake()     {         //check if instance exists         if (instance == null)              //if not, set instance             instance = this;          //if instance exists , it's not this:         else if (instance != this)              //then destroy this. enforces singleton pattern, meaning there can ever 1 instance of sceneloadersingleton.             destroy(gameobject);              //sets not destroyed when reloading scene         dontdestroyonload(gameobject);                 } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -