0
\$\begingroup\$

In my game I have several house groups (see image below), I want to show a specific message when the mouse hovers a specific house group.

For some reason, this only works on the first switch case (house) but fails on the second switch (house2). The default case doesn't work either. I verified that the case house2 is found by the engine. By not working I mean the text does not appear.

Do you have any idea about the problem?

Image

Code:

public class MouseIsOver : MonoBehaviour {

    private Image TipImage;
    private Text textObject;
    public string text;
    private bool displayInfo;

    void Start ()
    {
        TipImage = GameObject.Find("realtyInformation").GetComponent<Image>();
        textObject = GameObject.Find("realtyInformationText").GetComponent<Text>();
        TipImage.enabled = false;
    }
    void Update();  
    {
        Display();
    }

    void Display ()
    {
        if (displayInfo)
        {
            switch (text.ToLower())
            {
                case "house":
                    textObject.text = "house text1";
                    break;
                case "house2":
                    textObject.text = "house text2";
                    break;
                default:
                    textObject.text = "Click on the building";
                    break;
            }

        }
        else
        {
            TipImage.enabled = false;
            textObject.text = "";
        }
        TipImage.enabled = true;
    }

    private void OnMouseOver()
    {
        displayInfo = true;
    }

    private void OnMouseExit()
    {
        displayInfo = false;
    }
}
\$\endgroup\$
1
\$\begingroup\$

The trouble here is that you have multiple hoverable objects, each independently trying to take complete control of the text field. So whichever one happens to run last in the update order will win, even if it's not currently hovered.

Let's say "house2" is currently under the mouse, and updates before "house".

"house2"'s displayInfo is true so it sets textObject.text = "house text2"

Next "house" updates. Its displayInfo is false so it sets textObject.text = "", erasing the text that "house2" had set.

Instead, you'll want to keep responsibility for updating the text in one place. eg.

public class TipDisplay : MonoBehaviour {

    public static TipDisplay instance { get; private set; }

    public Text text;
    GameObject selected;

    void Awake() {
         instance = this;
    }

    public void SetTip(string tip, GameObject selected) {
         this.selected = selected;
         text.text = tip;
    }

    public void ClearTip(GameObject unselect) {
         if(unselect == selected) {
              selected = null;
         }
    }

   void Update() {
        if(selected == null)
             text.text = "";
   }
}

Put this on your text field and wire up the reference to the text component.

Now your individual hoverables can just signal when they become hovered / not hovered:

void OnMouseEnter() {
    TipDisplay.instance.SetTip(myTipText, gameObject);
}

void OnMouseExit() { 
    TipDisplay.instance.ClearTip(gameObject);
}

Passing the game object we're selecting/deselecting helps guarantee that we don't accidentally clear somebody else's tip if the calls happen in a different order than we expect.

\$\endgroup\$
0
\$\begingroup\$

Your Display() method isn't being called from anywhere (well, it is, but in a static block that runs once), either you have left out the void Update() in your question, or you have a bug there.

Second, I wouldn't use a switch block on a string. I would just put the desired display text in the text field and display it directly.

Third, you have TipImage.enabled = true; at the end of your Display() method, meaning that the tooltip will always display. You need to move that line inside your if (displayInfo) block.

\$\endgroup\$
  • \$\begingroup\$ Yes I deleted the Update function by mistake. \$\endgroup\$ – Grow Animation Feb 2 '18 at 18:48
  • \$\begingroup\$ @GrowAnimation Is the excess ; another typo? ;) Did my other critiques have any effect on the result? \$\endgroup\$ – Draco18s Feb 2 '18 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.