Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by NerdRageStudios · Jun 07, 2017 at 03:09 AM · uibuttoneventsystem

How to set UI button as selected on mouse over

Ok, so I have a problem that my game uses mouse, keyboard and joypad input. When the UI is activated, it always selects the first option in the UI, and you can control via gamepad fine.

However, when you move the mouse over any other option, it highlights, but the first object is still selected, thus making it look like two buttons are active.

I want to deselect the currently selected gameobject and select the one the mouse is hovering over.

I tried using the following code, but it doesnt seem to work, and I cant figure out why.

 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class MouseSelector : MonoBehaviour, IPointerEnterHandler {
 
     private void Start()
     {
         DontDestroyOnLoad(this);
     }
 
     public void OnPointerEnter(PointerEventData eventData)
     {
         if (eventData.pointerCurrentRaycast.gameObject != null)
         {
             EventSystem.current.SetSelectedGameObject(eventData.pointerCurrentRaycast.gameObject);
         }
     }
 }

Please can someone help?

Comment
Add comment · Show 1
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 joebain · Sep 25, 2019 at 09:50 AM 0
Share

I have this same problem. I think if an item is playing it's "highlight" animation then it should be the selected object in the event system, but for whatever reason it doesn't work like that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by IgorAherne · Jun 07, 2017 at 03:39 AM

i think your rect transforms are overlapping.

I was going to mention using additional piece of code, such as IPointerExitHandler, but SetSelectedGameObject should override whatever is selected. Hence, capturing the "pointer leaving previous button" is not necessary.

check if your rect transforms of buttons are overlapping, or something is in front of it

Comment
Add comment · Show 3 · 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 NerdRageStudios · Jun 07, 2017 at 03:45 AM 0
Share

Hey, thanks for replying :)

I tried that, doesnt seem to be the case, especially as the buttons are highlighting, which means the mouse must be triggering them....

avatar image NerdRageStudios · Jun 07, 2017 at 03:53 AM 6
Share

Well, I cant find an elegant solution to this, so ended up using this code from another thread, and attaching it to each button... it works

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 
 [RequireComponent(typeof(Selectable))]
 public class HighlightFix : $$anonymous$$onoBehaviour, IPointerEnterHandler, IDeselectHandler
 {
     public void OnPointerEnter(PointerEventData eventData)
     {
         if (!EventSystem.current.alreadySelecting)
             EventSystem.current.SetSelectedGameObject(this.gameObject);
     }
 
     public void OnDeselect(BaseEventData eventData)
     {
         this.GetComponent<Selectable>().OnPointerExit(null);
     }
 }
avatar image Parzival NerdRageStudios · Sep 09, 2020 at 06:30 PM 0
Share

Thank you! I was having the same problem and this fix worked for me.

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

108 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

Related Questions

RPG Maker style menu with UI buttons that are selected by controller 0 Answers

How do you highlight/select a button upon mouse enter and keep it moused over upon mouse exit? -2 Answers

How to make worldspace buttons work? 1 Answer

Create a new event for Button UI using script. 1 Answer

Detect UI Button Click Event in Update method 3 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