Search Unity

Clicking a button leaves it in MouseOver state

Discussion in 'UGUI & TextMesh Pro' started by kingdutka, Dec 11, 2014.

  1. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    I was contacted by staff after reporting this issue, they really wherent giving it a **** as they thought this was a low priority thing, yeah sure, hope they get ***** up so badly as us with a similar non priority issue, like toilet paper shortage in the office...
     
    angiemon likes this.
  2. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    Have you had the chance to test it yet? How does it work exactly and does it get rid of the problem and make the buttons turn their normal color after a click?
     
    ModLunar likes this.
  3. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    Haha.. Unfortunately not because my project got messed up while upgrading, the one time I forgot to make a backup!! So I'm staying on non-beta and non-alpha versions for now. .. sorry to end the hype, but once they document/explain it in a more stable release, then we can celebrate!!
     
    Last edited: Aug 30, 2021
  4. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    In the meantime, though, I've been using a slightly modified version of JoeStrout's solution, which has definitely helped! It's not perfect, because if you click off of all the selectable objects in-game, you can end up with nothing being selected. But this prevents 2 of the selectable objects from being highlighted at the same time.

    Put this script on all objects with a Selectable Component that should deselect all other objects when the current one is selected. (This works on Selectables, Buttons, InputFields, Dropdowns, etc.)

    XD thanks again @JoeStrout!


    Code (CSharp):
    1. //Work-around for Unity's failure to deselect the last-clicked UI
    2. //element when it highlights something else. See:
    3. //https://forum.unity3d.com/threads/285167/
    4.  
    5. //Special thanks for JoeStrout from the Unity forums for supplying this
    6.  
    7. using UnityEngine;
    8. using UnityEngine.EventSystems;
    9. using UnityEngine.UI;
    10.  
    11. [RequireComponent(typeof(Selectable))]
    12. public class HighlightDesuckifier : EventTrigger {
    13.  
    14.     private Selectable selectable;
    15.  
    16.     public void Awake() {
    17.         selectable = GetComponent<Selectable>();
    18.     }
    19.  
    20.     public void Desuckify() {
    21.         EventSystem e = EventSystem.current;
    22.         if (selectable.interactable && e.currentSelectedGameObject != gameObject) {
    23.             //Somebody else is still selected?!? Screw that. Select us now.
    24.             e.SetSelectedGameObject(gameObject);
    25.         }
    26.     }
    27.  
    28.     public override void OnPointerEnter(PointerEventData eventData) {
    29.         Desuckify();
    30.     }
    31. }
     
    Last edited: Jul 10, 2019
    SimRuJ and JoeStrout like this.
  5. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    Please keep us updated then.
    I'm definitely going to wait until an actual stable release of version 2019 is out to upgrade from my 2017.3, so it better be worth it. ;)

    In the meantime I'm using this (my original post: click):
    Code (CSharp):
    1. EventSystem.current.SetSelectedGameObject(null);
    It simply makes the EventSystem forget its currently selected button onClick. It works (unless you're using a touchscreen) and I haven't noticed too much weird behavior yet but it's still a pain in the butt to remember putting that in every onClick. Supporting a controller still requires slightly different handling.

    "Desuckify"... xD
     
    ModLunar likes this.
  6. Neiyra

    Neiyra

    Joined:
    Apr 19, 2016
    Posts:
    28
    This worked for me until yesterday. Then I upgraded to 2018.3.12, and now it throws a null reference exception, so I had to implement a workaround. :(
     
    Last edited: Apr 15, 2019
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Hmm... it's a little off-topic for this thread, but the standard way to do that is to just enable/disable the GameObject that's the root of the menu. I.e., call SetActive on it with true to show it, or false to hide it. This is much more efficient than moving objects far away.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Ah, no, this thread is about something different — when you click a button, it's left in the Highlighted state, even as you mouse over (thereby highlighting) something else. This is a bug (or serious design flaw) in Unity that hasn't been fixed in all these years. It has nothing to do with activating/deactivating items; you can see it just by throwing two buttons onto a canvas, with visible highlight style, and poking at them with the mouse.
     
    ModLunar likes this.
  9. Tidvis_Martin

    Tidvis_Martin

    Joined:
    Mar 12, 2019
    Posts:
    24
    HAHAHA :D
    Goes to show how much I actually know! Great intelligence on my part, I've been following the advice of a thread for a completely different problem, no wonder none of them helped :D

    Sorry to have disturbed this place, I'll remove my comments, maybe make my own post for posterity since what I was describing was still an issue and maybe the code, simple as it may be, could help someone some day.

    Again, sorry to have disturbed, thanks for clearing that one up @JoeStrout

    :D:D:D
     
    Last edited: Apr 21, 2019
    ModLunar likes this.
  10. the_maka

    the_maka

    Joined:
    Aug 1, 2019
    Posts:
    1
    I didn't see this behavior, what I saw was that when you click on a button, it is selected, and it stays in the selected state until something else is clicked (something else is selected).
     
  11. madruytb

    madruytb

    Joined:
    May 10, 2018
    Posts:
    26
    Same, I would like to learn how to fix this or why does this happens in Unity since ever?
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Then read above. The issue is explained and a solution is given.
     
  13. Tiberius1701

    Tiberius1701

    Joined:
    Sep 16, 2014
    Posts:
    71
    For those of you still trying to fix this, I ran into it tonight in an XR project. Tried every solution and nothing worked, but I dug a little deeper and watched the button and found that the Image component was getting permanently set to the highlighted color after press - I wrote a simple script that stepped through the buttons after press and set the image color back to white, and for now (fingers crossed), that solved the problem.
     
  14. akkiakash157

    akkiakash157

    Joined:
    May 1, 2023
    Posts:
    1
    I fixed this issue by turning off the animator component so that the animation will not get changed unless u click another button