0
\$\begingroup\$

Trying to understand the Unity UI system.

I have a panel that contains a button that should hide the panel.

When I wire the button up to the method that hides the panel, I can see in the console that the method is called but it has no effect.

However if I call an intermediate method from the button that uses the monobehavior Invoke method to call the disable method, even with a delay of 0, it does work.

Does not work:

    public void DisableUI(string sentBy)
    {
        print("disabling UI from: " + sentBy);
        _panel1.gameObject.SetActive(false);
        _secretPanel.gameObject.SetActive(true);
        _uiActive = false;
    }

Does work:

    public void DisableUI(string sentBy)
    {
        print("disabling UI from: " + sentBy);
        Invoke(nameof(SetUiInactive), 0f);
    }

    private void SetUiInactive() 
    {
        _panel1.gameObject.SetActive(false);
        _secretPanel.gameObject.SetActive(true);
        _uiActive = false;
    }

Note that calling the function directly from another script does work as expected.

Can anyone explain what's going on?

Thanks for any help.

\$\endgroup\$
3
  • \$\begingroup\$ Is it possible you have another script running that's re-enabling the UI immediately after this one runs, so delaying the disabling until the next frame ensures your work isn't immediately undone? \$\endgroup\$ Commented Jan 21, 2022 at 4:10
  • \$\begingroup\$ I don't believe so as I'm not getting anything in the terminal. I even put in that "sentBy" argument so any script that calls the method must send a message with it to make it easier to track down. \$\endgroup\$ Commented Jan 25, 2022 at 4:32
  • \$\begingroup\$ Then we'll need you to edit this question to include a Minimal Complete Verifiable Example. Create a new project, and add to it the bare minimum amount of code or scene objects needed to reproduce this problem. Then share those exact repro steps. Once we can replicate the problem, we can test potential fixes to be sure they'll work for you. \$\endgroup\$ Commented Jan 25, 2022 at 4:41

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.