[Solved] Global accesor problem.
Posted: Thu Oct 11, 2018 4:56 pm
Hi,
I have attached a UI prefab inside the Dialogue Manager -> Instantiate prefabs -> prefabs.
Inside the UI parent, I have a script which I am interested to make it as a "global accessor". So every script inside the game, if it needs it, they can access to their functions. To test if it can access various scripts, I have written one function that changes the UI visibility:
Then, Inside the canvas (provisional place) I have a script that calls the SetOpen function from the previous script:
Inside the UI, when it is visible, you can see a button. When you click it, it calls a load level function from a new script inside itself:
The problem is that, when It loads the level (itself), the MManager.GetInstance() seems to have lost its reference. Any idea of why of that? (Sorry, I am a newbie to coding).
Thanks!
I have attached a UI prefab inside the Dialogue Manager -> Instantiate prefabs -> prefabs.
Inside the UI parent, I have a script which I am interested to make it as a "global accessor". So every script inside the game, if it needs it, they can access to their functions. To test if it can access various scripts, I have written one function that changes the UI visibility:
Code: Select all
public class MManager : MonoBehaviour {
private static MManager m_Instance;
public static MManager GetInstance()
{
return m_Instance;
}
// All the child panels
public GameObject m_Child;
private void Awake()
{
m_Instance = this;
}
public void SetOpen(bool value)
{
m_Child.SetActive(value);
}
}
Code: Select all
public class TestMCall : MonoBehaviour
{
private void Update()
{
if (MManager.GetInstance() != null)
{
if (Input.GetKeyDown(KeyCode.E))
{
if (!MManager.GetInstance().m_Child.activeInHierarchy)
MManager.GetInstance().SetOpen(true);
else
MManager.GetInstance().SetOpen(false);
}
}
}
}
Code: Select all
public class MLoadScene : MonoBehaviour {
public void LoadLevel(int scene)
{
StartCoroutine(LoadAsync(scene));
}
IEnumerator LoadAsync(int scene)
{
MManager.GetInstance().SetOpen(false);
AsyncOperation async = SceneManager.LoadSceneAsync(scene);
while (!async.isDone)
{
yield return null;
}
}
}
Thanks!