-2

I am currently working on a game in Unity and I have the following problem:

I have a gameobject (a Panel) and on this panel I have multiple TextMeshProUGUI displaying "Save", "Load", "Options" and "Quit". I want to make it so, that when the player hovers with the mouse over one of these objects, the fontcolor changes or the glow goes up. However, I am unable to get a hold of how to actually make it happen. Whenever I start the game the console prints all the logs even bevore I have hovered over the objects. And when I do it afterwards, the logs are not printed anymore.

So far I have the following code:

public class OptionsHoverSkript : MonoBehaviour
{
    
    // Start is called before the first frame update
    void Start()
    {
        OnMouseEnter();
        OnMouseExit();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnMouseEnter()
    {
        // Get the game object.
        GameObject[] initialColorText = GameObject.FindGameObjectsWithTag("HoverText");

        foreach (var texts in initialColorText)
        {
            // Get the TextMeschProUGUI component from the object.
            TextMeshProUGUI newColorText = texts.GetComponent<TextMeshProUGUI>();

            string anotherText = newColorText.text;

            if (anotherText == "Save")
            {
                Debug.Log("Log1111111111111");
            }
            else if (anotherText == "Load")
            {
                Debug.Log("Log222222222");
            }
        }
        
        // Make it glow.
        // newColorText.fontSharedMaterial.SetColor(ShaderUtilities.ID_GlowColor, new Color32(215, 127, 60, 255));
    }

    private void OnMouseExit()
    {
        Debug.Log("ARGH / ANGRY ARNOLD VOICE!!");
    }
shingo
  • 18,436
  • 5
  • 23
  • 42
Malgosh
  • 340
  • 1
  • 11
  • 3
    "Whenever I start the game the console prints all the logs even bevore I have hovered over the objects" - well you're calling OnMouseEnter from the Start method... – Jon Skeet Jan 09 '23 at 21:15
  • Ok. And what method should I use in this case? If I call it on Update() I will get infinite prints. – Malgosh Jan 09 '23 at 21:24
  • 1
    Just like Update and Start OnMouseEnter will happen when the conditions are met automatically. You dont call them – BugFinder Jan 09 '23 at 21:35
  • I tried this yesterday already. I also tried adding coliders to the TextMeshProsUGUI objects and just debug.log() an entry. But it's not working. – Malgosh Jan 10 '23 at 05:43
  • 1
    You shouldn't be calling this manually at all .. it is an event message that will be called by Untiy – derHugo Jan 10 '23 at 06:05

1 Answers1

0

There are two problems here. First the one mentioned in the comments. You do not manually call OnMouseEnter() like others in the comments have said.

But since you said it was not being called before we can assume you are using the new Input System not the older Input Manager Quick start guide

In the future you should provide more details for people to provide you with an answers. The Unity Version and used packages are important to know.

If my assumption is correct changing your code in the following way should produce the desired result.

public class OptionsHoverSkript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        // Get the game object.
        GameObject[] initialColorText = GameObject.FindGameObjectsWithTag("HoverText");

        foreach (var texts in initialColorText)
        {
            // Get the TextMeschProUGUI component from the object.
            TextMeshProUGUI newColorText = texts.GetComponent<TextMeshProUGUI>();

            string anotherText = newColorText.text;

            if (anotherText == "Save")
            {
                Debug.Log("Log1111111111111");
            }
            else if (anotherText == "Load")
            {
                Debug.Log("Log222222222");
            }
        }
        
        // Make it glow.
        // newColorText.fontSharedMaterial.SetColor(ShaderUtilities.ID_GlowColor, new Color32(215, 127, 60, 255));
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("ARGH / ANGRY ARNOLD VOICE!!");
    }

More detail can be found in this very detailed answer.

ephb
  • 594
  • 1
  • 15
  • 1
    Thanks a lot man. I am really new to Unity programming. And still have to learn a lot of things. But thanks again for letting me know for when I post another question. I will keep that in mind :) – Malgosh Jan 12 '23 at 21:39
  • Glad I could help. This is a very common problem because a lot of older tutorials will not work anymore with the newer versions of Unity because the default packages and settings have changed. And since an Event not being called does not give an error this leaves new users wondering. – ephb Jan 12 '23 at 21:46