2

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;

 }
}
Dip Hasan
  • 225
  • 3
  • 9
  • 1
    Please be detailed in your question. What do you mean by "Won't enable"? Use `Debug.Log` to see what exactly is going on. Check if the canvas variable is null. Check in the Editor and tell us if the Canvas Object is still there in the Hierarchy. Add these to your question. Help us help you. – Programmer Aug 20 '17 at 19:35
  • @Programmer Hope it hels – TiZaLjubavNisiRodjena Aug 20 '17 at 19:59
  • @Programmer Or is it possible to not lose reference to my canvas. So I dont have to make it a child. so I can used it always like a saved variable which I enable/disable as it is a canvas – TiZaLjubavNisiRodjena Aug 20 '17 at 23:43

1 Answers1

1

Simply use code below to make singletons and get public GameObject references of a sliders or whatever you want to activate-deactivate when scene loading. For example if you have game manager that controls components of canvas, make both singleton and reference objects one to another. Source of code is here.

public class SceneLoaderSingleton : MonoBehaviour
{

    //Static instance of SceneLoaderSingleton which allows it to be accessed by any other script.
    public static SceneLoaderGameObject instance = null;    

    //Awake is always called before any Start functions
    void Awake()
    {
        //Check if instance already exists
        if (instance == null)

            //if not, set instance to this
            instance = this;

        //If instance already exists and it's not this:
        else if (instance != this)

            //Then destroy this. This enforces singleton pattern, meaning there can only ever be one instance of a SceneLoaderSingleton.
            Destroy(gameObject);    

        //Sets this to not be destroyed when reloading scene
        DontDestroyOnLoad(gameObject);            
    }
Ali
  • 101
  • 1
  • 9