How can handle UI.text object in unity by using method?

Viewed 36

I'd like to update UI.text by using UIManager methods.

This is UIManager script.

public class UIManager : MonoBehaviour
{
    private static UIManager m_instance;
    public static UIManager instance
    {
        get
        {
            if (m_instance == null)
                m_instance = FindObjectOfType<UIManager>();
            return m_instance;
        }
    }

    public void UpdatePlayerInfo(ref Text refText, string playerID, long money)
    {
        refText.text = "ID: " + playerID + "\n" + "money: " + money;
        refText.gameObject.SetActive(false);
    }

And I called UIManager method in Player script, like following code.

/* player script */
public bool Batting(int batIndex)
    {
        long toBat;

        ...
        money -= toBat;
        betMoney += toBat;
        
        // Here!
        UIManager.instance.UpdatePlayerBat(ref handText, betMoney);
        UIManager.instance.UpdatePlayerInfo(ref playerInfoText, playerName, money);
        return (true);
    }

But, it couldn't be updated.
I guess, it is a problem associate with Text object when using it parameter.
I already tried Text object with "ref" keyword and without "ref" keyword. But I don't know why...

Can't I use Text object in parameter ?
Or, is there any way that I can handle Text object by using method ?

Thank you !

1 Answers

Omg, it was my fault. The code is not need "ref" keyword.

But I forgot add UIManager object in hierarchy. The function is working well when I add UIManager object in gameobject !!

Related