Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 gangafinti · Mar 11, 2015 at 09:26 PM · ui

How can I check if a UI button is selected?

How can I check in script if a Button bla = new Button(); is selected? Not clicked on but just selected.

Thanks in advance :)!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
11

Answer by Estecka · Mar 12, 2018 at 05:59 PM

Compare your button's gameObject with EventSystem.current.currentSelectedGameObject. EventSystem requires the namespace UnityEngine.EventSystems

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
5

Answer by k0fe · Feb 22, 2020 at 11:44 AM

There are a few ways you can do this.

1) You can use EventSystems and subscribe to OnSelect event Documentation Reference

 using UnityEngine.EventSystems; // Required when using Event data.
 
 public class ExampleClass : MonoBehaviour, ISelectHandler // required interface for OnSelect
 {
     //Do this when the selectable UI object is selected.
     public void OnSelect(BaseEventData eventData)
     {
         Debug.Log(this.gameObject.name + " was selected");
     }
 }

2) You can use EventSystem to compare a gameObject with selected game object

     public GameObject ButtonGameObject;

     public void Update()
     {
         // Compare selected gameObject with referenced Button gameObject
         if(EventSystem.currentSelectedGameObject == ButtonGameObject)
         {
             Debug.Log(this.ButtonGameObject.name + " was selected");
         }
     }

Using gameobject comparison in Update is inefficient and kinda silly if your game is not built on selecting UI buttons only. So your best bet is to mix up first soultion with a comparison idea.

 using UnityEngine.EventSystems;
 
 public class ExampleClass : MonoBehaviour, ISelectHandler
 {
     public void OnSelect(BaseEventData eventData)
     {
         if(eventData.selectedObject == ButtonGameObject)
         {
             Debug.Log(this.ButtonGameObject.name + " was selected");
         }
     }
 }

3) Last but not least. You simply listen to OnMouseEnter/OnMouseExit and do your stuff. It's not a 100% way to check if a button was selected, but in most cases it works.

 public bool Selected;
  
 void OnMouseEnter()
 {
     selected = true;
     Debug.Log("Button was selected");
 }
  
 void OnMouseExit()
 {
     selected = false;
     Debug.Log("Button was deselected");
 }
Comment
Add comment · Show 1 · 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 B4ttleCat · Jan 22, 2021 at 10:57 AM 0
Share

Thank you so much. This worked perfectly. I also implemented the IDeselectHandler interface to add a behaviour there too.

avatar image
1

Answer by BMRX · Mar 11, 2015 at 11:28 PM

Wouldn't this work just fine?

 void OnMouseOver(){
     Debug.Log("Button Selected");
 }

Could also;

 void OnMouseEnter(){
     Debug.Log("Button Selected");
 }

 void OnMouseExit(){
     Debug.Log("Button Unselected");
 }

Bit better than my last answer.

Comment
Add comment · Show 4 · 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 Shiro_Rin · Apr 30, 2015 at 10:01 PM 1
Share

To add on to this, if you were trying to do something with it in game, you could add a bool to it also. so it could be like

 public bool selected;
 
 void On$$anonymous$$ouseEnter(){
 selected=true;
 Debug.Log("Button Selected");
 }
 
 void On$$anonymous$$ouseExit(){
 selected=false;
 Debug.Log("Button Unselected");
 }


That way you can set off certain events if the bool is true or false

avatar image BMRX · Apr 30, 2015 at 10:58 PM 0
Share

Very nice.

avatar image losingisfun · Nov 14, 2017 at 06:08 AM 2
Share

I must add, this does not answer the question properly. What if this is intended for controller support? or a mouse-free gameplay?

avatar image TooManySugar losingisfun · Nov 14, 2017 at 11:31 AM 2
Share

using UnityEngine.EventSystems;

 public class ExampleClass : $$anonymous$$onoBehaviour, ISelectHandler// required interface when using the OnSelect method.
 {
     //Do this when the selectable UI object is selected.
     public void OnSelect(BaseEventData eventData)
     {
         Debug.Log(this.gameObject.name + " was selected");
     }
 }
avatar image
1

Answer by freakrho · Oct 16, 2019 at 02:33 PM

Don't know if this is exactly what you wanted but it's what I wanted when I came across your question.

The class Selectable has the property currentSelectionState that you can check to see what state it's on, so to check if it's selected you would check

 currentSelectionState == SelectionState.Selected

(Button, Toggle, Slider, etc. all inherit from Selectable)

Comment
Add comment · Show 2 · 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 ackleyrc · Feb 02, 2020 at 12:17 AM 0
Share

Are you using an older version of Unity? It looks like the latest version(s) no longer expose this property

avatar image freakrho ackleyrc · Jan 22, 2021 at 02:04 PM 1
Share

I believe the property is protected, so you need to make a new class that inherits from what you need (ie: Button).

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.

Follow this Question

Answers Answers and Comments

29 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

Related Questions

Fading In and Out the GUI 1 Answer

Using events to change variables and inherit that value? 0 Answers

Passing through a GameObject/Function to a button's OnClick 1 Answer

How to trigger OnValueChange(Vector 2) method from code? 1 Answer

Making a Menu like UDK in Unity 4.6 1 Answer


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