- Home /
Unity 4.6 UI button highlight color staying after button clicked
I have a standard UI Button within a panel, and a different color for each of the transition states, to make it easy to identify which state it is in. Each state works as intended, except after I click on a button, the state transitions from the 'Pressed Color' back to the 'Highlighted Color'.
Unfortunately, it persists in this state even after moving the mouse off of the button. Clicking anywhere else within game sets the button back to the 'Normal Color' state.
I have adjusted different properties, but there really aren't that many related properties on a button that would affect this, so I am having trouble with it.
Answer by Dezky · Dec 17, 2014 at 01:18 PM
Got the problem too, it's due to the navigation system for keyboard only usage. put navigation on none on your button script and it will work ;)
Thanks for the answer, I'll try it out and see if it works for me.
This seems to work, but it's now doing some weird flicker, but I'm not too worried about that. Thanks for the help.
I had this problem with keyboard/game-controller-only usage on mobile.
When the button was pressed I switched to a submenu/page and disabled the button by disabling the parent GameObject for the entire menu. When I returning from the submenu the button would always still be highlighted because the animation of the button never got a chance to finish. The result is that the color of the button will stay forever in highlighted state.
The solution for this problem is to disable the Button (Button.enabled=false;) ins$$anonymous$$d of deactivating the entire GameObject and to disable the Canvas of the Button (Canvas.enabled=false;).
Answer by Robdon · Feb 16, 2016 at 09:36 PM
Rather than disabling navigation you can do this.
All you need to do is remove the currently selected object from the event system, since technically, the button is then the 'selected' object after clicking it. Its like the windows 'focus' rect around the current field.
using UnityEngine.EventSystems;
EventSystem.current.SetSelectedGameObject(null);
If you pop that into your buttonpressed script/method, then it will remove the selection and colour immediately.
This works great! Thanks.
Not sure about that OnPointerExit way mentioned in another answer.
Worked great for me (Unity 2017, had the exact same issue) and seems better than disabling navigation for sure
Thanks <3. Love the quick solution!
Answer by JuanMaldonado · May 21, 2017 at 06:05 PM
In my case, setting the navigation to NONE didn't work. The solution is to disable and re enable the button component and if you have animations, trigger the "Normal" animation transition.
GetComponent<Button> ().enabled = false;
GetComponent<Button> ().enabled = true;
GetComponent<Animator> ().SetTrigger("Normal");
This is the correct answer dont disable navigation it has other uses
Answer by lermy3d · Aug 27, 2015 at 02:02 PM
Wouldn't this be solved if they include a Hover state?
This will give you full control, you could make the state different from the Highlight state and users do not get confused with two highlighted elements and you can still keep using the keyboard/joystick navigation system.
In this case you could also have the choice to decide whether the button should be different in a hover state or stay the same if you do not want that behavior! :)
A better solution would be to simply not leave elements highlighted until the keyboard / joystick is employed while a UI element is interactable. Which is how most applications behave.
Is there any way to globally toggle the navigation system?
How about when the mouse move over a button triggering the highlight state and then you start using the keyboard, in the way is currently working you will have two highlighted elements.
If you set by default the behaviour you are explaining it would be okey, since is how you said "is like most applications work", but that still does not give the developer a way to create and customize a different user experience.
Having a hovering state allows the developer to control that behaviour without imposing a specific model.
Answer by RogDolos · Oct 21, 2015 at 10:39 PM
I think most importantly it should work correctly for one input at a time. The navigation features are especially important for those with disabilities using alternate controls (via keyboard interface typically). So making a custom user experience that involves a combo of mouse and keyboard at the same time seems counter-productive to me. This is exactly why I cringe whenever someone suggests the "fix" is to turn navigation off entirely.
Right now though, the way the navigation and hover uses the same highlight just causes confusion in most cases.
For your original suggestion, it could be achieved using an Event Trigger with PointerEnter and PointerExit and apply a temporary overrideSprite as a "hover".
Exactly, also it could be so awesome if that state were included and took into account as another ui state and shipped with Unity, nevertheless I could make such a script without any problem, if I doit I'll share it. All the best.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174049im_/https://answers.unity.com/themes/thub/images/avi.jpg)