OnMouseOver/Exit doesn't work together to Hover

I am trying to accomplish a hover state for when a mouse enters and exits the GameObject.
Only when the OnMouseExit is //commented out the function OnMouseOver() decides to work but the OnMouseExit will not. Both function will not work together.

…when the OnMouseOver() works the hover will not deactivate again. Which is why i am trying to use OnMouseExit()

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.UI;

public class NewBehaviourScript : MonoBehaviour
{
    public GameObject selected;
    public GameObject hover;


    // Start is called before the first frame update
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            selected.SetActive(true);
            hover.SetActive(true);

        }
    }

    void OnMouseOver()
    {
        hover.SetActive(true);
    }

    //     public void OnMouseExit()
    //     {
    //         hover.SetActive(false);
    //     }




}

6164142--674238--Screenshot 2020-08-03 at 17.45.12.png

If you’re using an event trigger, you shouldn’t also be using Unity “Magic Methods” like OnMouseOver and OnMouseExit. Unity - Scripting API: MonoBehaviour.OnMouseExit() Those methods are for 3d objects with colliders. Make your own method names so they don’t conflict with this separate concept in Unity.

As for your specific issue, you have both methods set as callbacks for the “Pointer Enter” event. You need to click the “Add a new event type” button and add a Pointer Exit listener, and add OnMouseExit to that event. As it is right now Unity will call both methods on pointer enter.

1 Like