Open/Close Panels on button clicks

Hey,

I’m trying to code some panels so that when a button is pressed it enabled the panels, and when the button is pressed again it disables the panels. (Later on I will also close the panels and open new ones, if a different button is pressed.
Right now though, I successfully managed to script the panels to open OnClick but they don’t close on click. When I try to code in the panels closing, OnClick will simply do nothing. I’ve tried using the inspector too but I get the same problem.

Here is the code currently;

[B]void disablePanel()[/B]
        {
            woodPanelUI.SetActive(false);
            woodWorkUI.SetActive(false);
        }
   

    [B]void enablePanel()[/B]
    {
        woodWorkUI.SetActive(true);
        woodPanelUI.SetActive(true);
    }

and here is the code when I try to do an if statement to determine whether the panels are open or not.

void disablePanel()

    {
        if enablePanel() = true; 

        woodPanelUI.SetActive(false);
        woodWorkUI.SetActive(false);
    }
       
   

    void enablePanel()
    {
        woodWorkUI.SetActive(true);
        woodPanelUI.SetActive(true);
    }

Using the if statement is just throwing numerous errors in my face about parenthesis expected and overload exceptions and explicit casting. So when I attempt to fix the errors it just brings up the same errors.

I also tried a different piece of code all together; one I much preferred:

void switchCanvas(bool disable)
    {
        if (!disable)
        woodPanelUI.SetActive(false);
        woodWorkUI.SetActive(false); [B]*[/B]
        else
        woodWorkUI.SetActive(true);
        woodPanelUI.SetActive(true);
    }

The only problem with this piece of code, is that for some reason it expects a } after the 3rd line of code (see the * marked.)

Any help would be great, and I’d much rather use the second code for speed in future development process of the project.

You don’t actually need code to enable and disable a GameObject. You can call SetActive directly from the inspector.

As to your second piece of code, you are missing braces. Put a { at the end of line 3 and 6, and a } at the end of line 5 and 8.

The problem with setting it in the inspector is that the same button opens and closes the panel, so setting it to both open and close the panel actually cancels itself out so it does nothing.

After putting in the {}'s the panel will open on click, but it will not close on click.

Use two identical looking buttons that swap.

How are you toggling disable?

I’m currently trying to sort it via the inspector (checking the box to make them active, not checking it so it become inactive) - but whether checked or not, the panels aren’t showing up - if they’re enabled at the start of play - they will disable, but if set to inactive on play - they will become active but wont show up on the screen.

Originally the code I wanted to put in place would activate the panels on the first press, and then press the button again to deactivate OR press on another button to close the “Wood” UI elements and open up another set of UI elements.

Edit: None of the UI elements will show up, but it will happily swap between both both buttons properly. The first button is supposed to open the UI elements as well as activate the second button, the second button closes the UI elements and reactivates the first. Both buttons will deactivate themselves when pressed.

I also added a script to each button that would SetActive true or false depending on the function of the buttons and set the function in the inspector - but still the panels aren’t showing up.

Ignore this. I had the panels set as children of one of the buttons for some reason. I’m an idiot.