Multiple options in one script error

Hey there, i have a script for (for now) 2 buttons, which both have to open a panel on click.
However, when i attach the required buttons/panels in the engine.
The second panel doesnt go inactive, and the button doesnt work.
The first panel and button DO work though… Whats wrong?


public class PlayerPanel : MonoBehaviour {

    public Button skillButton;
    public Button currencyButton;
 

    public GameObject skillPanel;
    public GameObject currencyPanel;



    void Start () {
        skillPanel.SetActive(false);
        Button skillB = skillButton.GetComponent<Button>();
        skillB.onClick.AddListener(openSkillPanel);

        currencyPanel.SetActive(false);
        Button curB = currencyButton.GetComponent<Button>();
        curB.onClick.AddListener(openCurrencyPanel);
    }
 

    void Update () {
     
    }

    void openSkillPanel()
    {
        if (skillPanel.active == false)
        {
            skillPanel.SetActive(true);
        }
        else
        {
            skillPanel.SetActive(false);
        }
    }

    void openCurrencyPanel()
    {
        if(currencyPanel.active == false)
        {
            currencyPanel.SetActive(true);
        } else
        {
            currencyPanel.SetActive(false);
        }
    }
}

The code part is fine, it might be the assignation on the editor. On another note try using code tags so your code is easier to read.
Using code tags properly page-2#post-3353071

Does your console output any errors?

Got an Unassigned reference exception, but it seems rather harmless (its from another script aswell).

Uhm about the editor, what could go wrong other then assigning the buttons/panels?
I attached the script to 2 buttons, and i assigned all panels/buttons to both scripted buttons.
(thx for the code tag advice btw!)

Sidenote:
If i apply all panels/buttons to both scripted buttons, non of them work.
If i apply panels/buttons only to the selected buttons(e.a. Skillpanel+button on the skillbutton script. and Currencypanel+button on the buttonscript)
then only the skill one works (the first one)

then you are either missing the button or the panel on the component, check your inspector on the editor

Maybe a bit more clear like this:

In this case skill panel opens (its simmilar like this currency panel)
but currency doesnt open

You only need the script one time. Thats the real problem. An unassigned reference is a huge error. But the problem here is that while one script is opening it, the other script then closes it because its executing after that.

When an error ocurres the rest of the lines after that don’t execute, so if you dont assing the skillbutton or panel the this doesn’t execute
I always write comments in caps so it stands out, don’t take it as anything.

skillPanel.SetActive(false);
        Button skillB = skillButton.GetComponent<Button>();//ERROR HERE

//NONE OF THIS EXECUTES
        skillB.onClick.AddListener(openSkillPanel);
        currencyPanel.SetActive(false);
        Button curB = currencyButton.GetComponent<Button>();
        curB.onClick.AddListener(openCurrencyPanel);

What you actually wanted i think its this

public class PlayerPanel : MonoBehaviour {
    public Button button;
    public GameObject panel;
    void Start () {
        panel.SetActive(false);
        button.onClick.AddListener(openPanel);
    }
    void Update () {
    
    }
    void openPanel()
    {
        if (panel.active == false)
        {
            panel.SetActive(true);
        }
        else
        {
            panel.SetActive(false);
        }
    }
}

This way you can have more panels with their respective buttons without coding more of them, you just need to add another component into the scene.

Ah only need to define it once it seems. Thx allot!

No problem, glad to help