Search Unity

Question How to get if UI button is currently highlighted?

Discussion in 'Scripting' started by EpicMcDude, Oct 21, 2021.

  1. EpicMcDude

    EpicMcDude

    Joined:
    Apr 15, 2013
    Posts:
    117
    Hi,

    Trying to figure out a simple way to know if a UI button is currently highlighted or not, this is for using a gamepad or keyboard, so I can't use OnPointerEnter or OnMouseOver methods or anything that exclusively uses a mouse.

    I'm not proficient in C#.

    I've been googling around but nothing seems to work so far -
    https://answers.unity.com/questions/1629490/checking-if-button-is-highlighted-with-a-controlle.html
    This one only works once the button is selected, not highlighted.

    https://docs.unity3d.com/2017.3/Documentation/ScriptReference/UI.Selectable.IsHighlighted.html
    This seems to be the closest thing to what I want, but I can't seem to make this work, I get the error "No overload method for "IsHighlighted" takes 1 arguments" and I have no idea how to fix it.

    Thank you!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    What's your code look like?
     
  3. Lambo_swiper5

    Lambo_swiper5

    Joined:
    Feb 2, 2021
    Posts:
    4
    did you guys figure it out? I also want to check if it's highlighted. I'm using unity 2021.2, but my error says The name 'isHighlighted' does not exist in the current context.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. public class IsSelected : Selectable
    7. {
    8.     BaseEventData BaseEvent;
    9.     public GameObject dot;
    10.  
    11.     void Update()
    12.     {
    13.         if (isHighlighted(BaseEvent) == true)
    14.         {
    15.             dot.SetActive(true);
    16.         }
    17.         else
    18.         {
    19.             dot.SetActive(false);
    20.         }
    21.     }
    22. }
    23.  
    is my code.
     
  4. C# is a case sensitive language. It is
    IsHighlighted
    .
     
  5. Lambo_swiper5

    Lambo_swiper5

    Joined:
    Feb 2, 2021
    Posts:
    4
    now I have the error No overload for method 'IsHighlighted' takes 1 arguments
     
  6. Lambo_swiper5

    Lambo_swiper5

    Joined:
    Feb 2, 2021
    Posts:
    4
    Last edited: Nov 9, 2023
  7. Lambo_swiper5

    Lambo_swiper5

    Joined:
    Feb 2, 2021
    Posts:
    4
    nevermind I used chatgpt cause I wanted to make this work quickly and got it working.
    If anyone has this problem imma leave my script.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4.  
    5. public class IsSelected : MonoBehaviour, ISelectHandler, IDeselectHandler
    6. {
    7.     private bool isHighlighted = false;
    8.     public GameObject dot;
    9.  
    10.     public void OnSelect(BaseEventData eventData)
    11.     {
    12.         isHighlighted = true;
    13.         dot.SetActive(true);
    14.         Debug.Log("Button Highlighted");
    15.     }
    16.  
    17.     public void OnDeselect(BaseEventData eventData)
    18.     {
    19.         isHighlighted = false;
    20.         dot.SetActive(false);
    21.         Debug.Log("Button Unhighlighted");
    22.     }
    23.  
    24.     // You can use isHighlighted in other parts of your script as needed.
    25. }
     
  8. patrick_Drake

    patrick_Drake

    Joined:
    Jul 2, 2023
    Posts:
    18
    Umm just to clear this up, you would use IsHighlighted like a bool, all you had to do was put the selectabke Before it and take the BaseEvent out of the parenthesis for it to return correctly. I dont recommend anyone use chatgpt ever. It's only worthwhile to write boilerplate if u akready know what ur doing, otherwise you wont learn anything.
     
    PraetorBlue likes this.