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
0
Question by LeMorrow · Jun 13, 2017 at 11:49 PM · c#uibuttonseventsystemhighlight

EventSystem.SetSelectedGameObject Does not display highlight animations for my buttons

I have 3 buttons in a vertical layout in a panel. When I click a button on another object I want to enable the panel, with the middle button selected. This works perfect the first time around. I can use my up and down arrows to navigate the buttons, and all of them display their highlight animation. However, when I disable the panel and re-enter, glitchy things happen. Basically, sometimes one of the buttons are highlighted for a frame and then return to their idle state. When you highlight that button again it's highlight animation may work, at random chance. And sometimes just none of the highlight animations work.

Also, disabling and re-entering the panel a third time will guarantee that none of them work as far as I know. However, as I included in my code, I check for the highlighted object and they are highlighted as I want them to, they're just missing the animations.

I've tried with the Coroutine fix from another post but I'm probably doing something wrong.

This script is attached to the panel with the 3 buttons.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class Highlight : MonoBehaviour 
 {
 
     public EventSystem eventSystem;
     public Button myButton;
 
     void OnEnable()
     {
         StartCoroutine (HighlightButton ());
     }
 
     void Update()
     {
         if (eventSystem.currentSelectedGameObject != null)
         {
             print (eventSystem.currentSelectedGameObject.name);
         }   
         // This prints the correct buttons, but the highlight
         // animations doesn't display most of the time.
     }
 
     void OnDisable()
     {
         eventSystem.SetSelectedGameObject (null);
     }
 
     IEnumerator HighlightButton ()
     {
         eventSystem.SetSelectedGameObject (null);
 
         yield return new WaitForEndOfFrame ();
         eventSystem.SetSelectedGameObject (myButton.gameObject);
     }
 }


SIdenote for moderators: I've already posted this question, 7 days ago, but it's still awaiting moderation and I've noticed that you removed post-moderation since then, so I'm reposting it.

Tested in Unity version 5.6.0f3

EDIT: Problem ended up being much more simple than I thought, it was simply an empty highlight state. Thanks a lot to @TonyLi !

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by TonyLi · Jun 16, 2017 at 02:51 PM

Call the button's OnSelect() method. This plays its transition.

 // Deselect previous selection and play its deselect transition:
 if (eventSystem.currentSelectedGameObject != null)
 {
     var previous = eventSystem.currentSelectedGameObject.GetComponent<Selectable>();
     if (previous != null)
     {
         previous.OnDeselect(null);
         eventSystem.SetSelectedGameObject(null);
     }
 }
 // Select button and play its selection transition:
 eventSystem.SetSelectedGameObject (myButton.gameObject);
 myButton.OnSelect(null);
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 LeMorrow · Jun 16, 2017 at 03:32 PM 0
Share

Changed my script like you said, and nothing changed. Same output as before; the buttons get highlighted like they should for the first time around, but after having the menu disabled and re-enabled, the highlights won't work for 2 of the buttons. The third time around, none of them work.

Here's my new script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class Highlight : $$anonymous$$onoBehaviour 
 {
 
     public EventSystem eventSystem;
     public Button myButton;
 
     void OnEnable()
     {
         eventSystem.SetSelectedGameObject (myButton.gameObject);
         myButton.OnSelect(null);
     }
 
     void Update()
     {
         if (eventSystem.currentSelectedGameObject != null)
         {
             print (eventSystem.currentSelectedGameObject.name);
         }   
     
         // This prints the correct buttons, but the highlight
         // animations doesn't display most of the time.
     }
 
     void OnDisable()
     {
         if (eventSystem.currentSelectedGameObject != null)
         {
             var previous = eventSystem.currentSelectedGameObject.GetComponent<Selectable>();
             if (previous != null)
             {
                 previous.OnDeselect(null);
                 eventSystem.SetSelectedGameObject(null);
             }
         }
     }
 }

I've also tried putting the OnDisable actions in the script that deactivates the object, so it happens before the deactivation of the menu. Still the same result. I could provide a youtube video of what's happening, but I'm not at home and I'm using a mac so I don't have a video recorder. Screenshots wouldn't be very helpful.

Any other ideas?

avatar image TonyLi LeMorrow · Jun 16, 2017 at 05:44 PM 0
Share

The script below works for me. Here's an example scene: TestHighlight_2017-06-16.unitypackage

 using System.Collections;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class Highlight : $$anonymous$$onoBehaviour
 {
 
     public EventSystem eventSystem;
     public Button myButton;
 
     void OnEnable()
     {
         StartCoroutine(Select$$anonymous$$yButton());
     }
 
     IEnumerator Select$$anonymous$$yButton()
     {
         yield return null; // Wait 1 frame for UI to recalculate.
         if (eventSystem.currentSelectedGameObject != null)
         {
             var previous = eventSystem.currentSelectedGameObject.GetComponent<Selectable>();
             if (previous != null)
             {
                 previous.OnDeselect(null);
                 eventSystem.SetSelectedGameObject(null);
             }
         }
         eventSystem.SetSelectedGameObject(myButton.gameObject);
         myButton.OnSelect(null);
     }
 
     void OnGUI()
     {
         // You can also inspect the EventSystem to see the current selection.
         GUILayout.Label("Selection=" + eventSystem.currentSelectedGameObject);
     }
 
 }


avatar image LeMorrow TonyLi · Jun 17, 2017 at 08:06 PM 0
Share

Hey! Thanks for sticking up with me. Your scene works very well indeed, but here's a scene where I've changed so that Escape disables the menu, there isn't a "Back" button, and this seems to completely fuck everything up. I can't pin down the problem though, nor can I fix it. Any ideas?

Show more comments

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

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

EventSystem: how to highlight instantiated button? 1 Answer

Button Scroll view original default button becoming clone 0 Answers

Canvas-prefab elements are not interactable 2 Answers

Buttons don't work after Unity 2017 update 0 Answers

ScrollRect and IDragHandler on touch devices 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