0

I am trying to make my Canvas Manager work but it gives me the same error even after I make a Singleton. Here's my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class CanvasMgr : MonoBehaviour {
    [SerializeField]
    private GameObject defultUI;
    [SerializeField]
    private GameObject upgradeUI;

    public GameObject UpgradeUI { get => upgradeUI; set => upgradeUI = value; }
    public GameObject DefultUI { get => defultUI; set => defultUI = value; }

    public static void Refresh () { 
        if (ValueHolder.upgrademenuopen){
            DefultUI.SetActive (false);
            UpgradeUI.SetActive (true);
        }
        if(!ValueHolder.upgrademenuopen){
            DefultUI.SetActive (true);
            UpgradeUI.SetActive (false);
        }
    }
}

Edit: Thank you this is my final code: I ended up being stupid not going for the better c# course as I was lazy. If I can give you a lesson: Do not go for the easy "5 day coding courses" but rather go to "the comprehensive c# course". Thank you.

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class CanvasMgr : MonoBehaviour {
private CanvasMgr() { }
private static CanvasMgr instance = null;
public static CanvasMgr Instance
{
    get
    {
        if (instance == null)
        {
            instance = new CanvasMgr();
        }
        return instance;
    }
}

[SerializeField]
private GameObject defultUI;
[SerializeField]
private GameObject upgradeUI;

public GameObject UpgradeUI { get => upgradeUI; set => upgradeUI = value; }
public GameObject DefultUI { get => defultUI; set => defultUI = value; }

public void Refresh () { 
    if (ValueHolder.upgrademenuopen){
        DefultUI.SetActive (false);
        UpgradeUI.SetActive (true);
    }
    if(!ValueHolder.upgrademenuopen){
        DefultUI.SetActive (true);
        UpgradeUI.SetActive (false);
    }
 }
}

1 Answers1

3

This is not a singleton class. You should do something like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class CanvasMgr : MonoBehaviour {
    private CanvasMgr() { }
    private static CanvasMgr instance = null;
    public static CanvasMgr Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new CanvasMgr();
            }
            return instance;
        }
    }

    [SerializeField]
    private GameObject defultUI;
    [SerializeField]
    private GameObject upgradeUI;

    public GameObject UpgradeUI { get => upgradeUI; set => upgradeUI = value; }
    public GameObject DefultUI { get => defultUI; set => defultUI = value; }

    public void Refresh () { 
        if (ValueHolder.upgrademenuopen){
            DefultUI.SetActive (false);
            UpgradeUI.SetActive (true);
        }
        if(!ValueHolder.upgrademenuopen){
            DefultUI.SetActive (true);
            UpgradeUI.SetActive (false);
        }
    }
}`

Now you can call CanvasMgr.Instance.Refresh().

mcelik
  • 1,073
  • 2
  • 12
  • 22