• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Chrisv007 · May 07, 2019 at 05:00 PM · buttoncontrollerhighlight

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");
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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
         }
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is the 'highlighted state' the same as the 'selected state'? 2 Answers

AndoidTouchControllJavaRaycast 0 Answers

How to Play Sound on Button Highlight 2 Answers

How do I run a script when a button is highlighted 3 Answers

Android Dualshock Controller Button Mapping Issue 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges