Search Unity

Unity UI 2019.2+ Button.interactable issues

Discussion in 'UGUI & TextMesh Pro' started by NickD_axonom, Sep 25, 2019.

  1. NickD_axonom

    NickD_axonom

    Joined:
    Jan 8, 2014
    Posts:
    7
    Hello everyone, I am currently having an issue with setting a buttons interactability from false to true in Unity 2019.2. We set the button's interactability to false on the button click through the buttons event system's "OnClick" event (and it works!). Then in code if something fails (that happened in the button click) it will set it to true. In 2019.2 it seems this doesn't work anymore. The documentation for UnityEngine.UI doesn't exist anymore yet it still exists in the project which is a little confusing. Looking up and down the forums and searching the web doesn't yield any results. Did I miss something or is this weird behavior?

    Currently using Unity 2019.2.4f1.

    Thanks!
    Nick D.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    So you've set up the OnClick event of this button so that it has two different actions, one of which makes it non-interactable and the other calls some code you wrote that might eventually make it interatable?

    If that's the case, my first guess would be that the order of execution has changed; it used to mark the button as non-interactable and then call your code, but now it's calling your code first and then marking the button as non-interactable.

    I'm not sure if Unity is supposed to guarantee a particular order of execution of event listeners, but in this case I would suggest you just move all of the logic into your own code so that you can control the order yourself. (Your function apparently already has a reference to the button, so it should be able to set interactable = false before it does anything else. Or even just invert your logic at the end so that it ONLY sets interactable = false if whatever-you're-doing succeeds.)
     
  3. NickD_axonom

    NickD_axonom

    Joined:
    Jan 8, 2014
    Posts:
    7
    You got a point, I will double check first thing tomorrow. Thing is, the button is to login to our service which requires talking to a server so I figured it wouldn't wait with the next event until the first one is done.

    But am I overreacting that the whole UnityEngine.UI is missing from the 2019.2 documentation?
    https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Button.html works, but
    https://docs.unity3d.com/2019.2/Documentation/ScriptReference/UI.Button.html is 404'ing.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    In that case, it shouldn't be waiting if you used a reasonable implementation for talking to your server. (Assuming you correctly used async, coroutine, or some other non-blocking approach.) Putting the initial "interactable = false" part at the start of your function might still be a good idea, though.

    Otherwise, I guess I can only suggest standard debugging techniques: add Debug.Log statements so you can make sure the correct things are happening at the correct times, check the value of "interactable" in code and in the inspector, etc.
     
  5. NickD_axonom

    NickD_axonom

    Joined:
    Jan 8, 2014
    Posts:
    7
    So it turns out that the order of the event does indeed do it in order, but for an odd circumstance. Our login code is in an external dll which doesn't use async/enumerators so the belief is that it treats it as a sequence. I flipped them and I got the desired effect. But because of this the button I have doesn't show that its not interactable since the frames seem to skip until the login is done. Adding a few frames of waiting after the button click seems to solve this problem though!
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If your screen doesn't update at all while your login code is running, that means your login code is blocking the main Unity thread. You should avoid this if you possibly can--it means your entire application is freezing! Note that if your login code is unable to get a response from your server for some reason, your whole app could remain frozen until it times out!

    Consider invoking your DLL from a separate thread or from an async function.