Home > front end >  OnMouseEnter function prints all the logs at the same time (Unity and C#)
OnMouseEnter function prints all the logs at the same time (Unity and C#)

Time:01-13

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()
    {
        onm ouseEnter();
        onm ouseExit();
    }

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

    private void onm ouseEnter()
    {
        // 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 onm ouseExit()
    {
        Debug.Log("ARGH / ANGRY ARNOLD VOICE!!");
    }

CodePudding user response:

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.

  • Related