- Home /
Checking if button is highlighted with a controller (no mouse)
Hi guys, I'm using a controller button to highlight 3 buttons (ammo selection). The buttons get highlighted fine. The problem is that I need to execute a method when one of these buttons is highlighted. I don't want the player to have to highlight the button THEN press OK. I found a whole bunch of threads on this but they all seem to involve a mouse. My game is played with a controller. The question is: How can I achieve this? I tried all the scripts found here and there but none of them work. Here is the last option I found. That code transforms the button into something else which I can't access to make it intractable or not from another script. It also removes the OnClick that I would still like to keep for people not using a controller but using the keyboard instead. Any help would be appreciated. I spent all morning just trying to get this to work. I'm on 2018.3. Thanks.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//Use the Selectable class as a base class to access the IsHighlighted method
public class BuckShotSelected : Selectable
{
//Use this to check what Events are happening
BaseEventData m_BaseEvent;
void Update()
{
//Check if the GameObject is being highlighted
if (IsHighlighted(m_BaseEvent) == true)
{
//Output that the GameObject was highlighted, or do something else
Debug.Log("Selectable is Highlighted");
}
}
}
Answer by Fuzysasquatch · Nov 02, 2019 at 06:27 PM
I had a similar problem where I needed to execute a task when a certain button was highlighted with the controller instead of the mouse. I'm not sure if this is the best way to do this but so far this is the only way I can figure out how to do it. Use EventSystem.current.currentSelectedGameObject to store the current highlighted button. Then just simply check the gameObjects name and run desired method.
public class ClassName: MonoBehaviour
{
GameObject currentSelected;
void Update()
{
currentSelected = EventSystem.current.currentSelectedGameObject;
if(currentSelected.name == "NameOfButton")
{
... run desired code or method
}
}
}
Answer by Snotax · Nov 14, 2020 at 07:42 AM
@Chrisv007 @Fuzysasquatch The simplest way to implement this is with the ISelectHandler interface. You can just add the interface to a MonoBehaviour on the button and there you have it:
public class UIButton : MonoBehaviour, ISelectHandler{ public void OnSelect(BaseEventData eventData) { //DO STUFF } }
Your answer
![](https://koobas.hobune.stream/wayback/20220612200909im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How To Add Button Images To GUIText? 1 Answer
How to know if finger is on joystick if it is at horizontal & vertical zero 0 Answers
Help Needed: How to Use Input Manager on UI Button 0 Answers
How to make a menu controled from Keyboard and using buttons? 0 Answers
What is the mapping for the Right Stick on Xbox 360 Controllers? 1 Answer