Hi,

I’ve made my game to hide the cursor in menu when the user starts using keyboard or gamepad/controller. I also disable the mouse input for the UI. If the user moves the mouse, the cursor appears and mouse control is enabled.

There’s only one thing that’s bugging me. If a cursor/pointer is hovering over a button and then if I start using keyboard or gamepad for input, the pointer hides just fine, but the hovered button still has a hovered/selected appearance. So it just sits there looking selected even though I’m selecting other buttons with keyboard/gamepad.

It just looks silly, as if two buttons are selected.

Does anyone know how can I clear the effect of mouse hover once I hide and disable the cursor?

I didn’t even manage to get the gameobject that is hovered.

(PS Similarly, if a button is selected by keyboard/gamepad, and i start using the mouse, then again I have the same silly appearance as if two buttons are selected (one remains selected and the other is hovered). I’ve solved this problem easily by calling EventSystem.current.SetSelectedGameObject(null) when mouse control is activated. Then again, if I start using keyboard/controller, I manually set SelectedGameObject to some default button for that menu panel.)

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;

public class FirstMacScript : MonoBehaviour 
{
	void Update () 
	{
			PointerEventData pointer = new PointerEventData(EventSystem.current);
			pointer.position = Input.mousePosition;

			List<RaycastResult> raycastResults = new List<RaycastResult>();
			EventSystem.current.RaycastAll(pointer, raycastResults);

			if(raycastResults.Count > 0)
			{
				Debug.Log (raycastResults [raycastResults.Count-1].gameObject);
			}
	}
}

Old thread, but I ended up here for the same question in 2022.

The answer from wlad_s is still working (thanks again), but I found another solution that works for me and wanted to share it in case it can help someone else.

I’m using Rewired, but pretty sure it can be done without it too.


    // Put this code in a Controller changed callback
	if (controller.type != ControllerType.Mouse)
	{
		Cursor.lockState = CursorLockMode.Locked;
		Cursor.visible = false;    
	}
	else if (controller.type == ControllerType.Mouse)
	{
		Cursor.lockState = CursorLockMode.None;
		Cursor.visible = true;
	}