Making text on a UI canvas disappear and reappear

I’m trying to make text that says “robbing…” reappear and disappear when showRobText is false. The text is simply an empty gameObject with a text component on it.

This code doesn’t seem to be working though.

What did I do wrong?

    public Text ValueText;
    public Text robText;
    public robManage script;
    private bool showRobText = false;
    public Text robbingLabel;

    void Update()
    {
        ValueText.text = script.playerMoney.ToString();
        if (script.isRobbing == true)
        {
            showRobText = true;
        }
        if (showRobText == true)
        {
            robText.text = "Robbing...";
            Invoke("disableRobText", 3);
        }
    }

    private void disableRobText()
    {
        showRobText = false;
        if (showRobText == false)
        {
            robbingLabel = GetComponent<Text>();
            robbingLabel.enabled = false;
        }
    }

}

There are a few errors.

  1. One is that you are potentially setting enabling and disabling the wrong text. Remove robbingLabel variable and replace it with robText. Ensure that you point to the correct text component in the editor.
  2. The second issue is that you are doing this in an Update function. This is a problem as for every update that script.isRobbing is true you will be running the the disableRobText and robText.text functionality. Meaning you are going to disableRobText on numerous frames.
  3. Another error is that while you are setting the robText to false you are not setting it to true before hand. So if this event is repeatable you are always going to have a disabled Text Component after the first run.

Let me know if you need more assistance.