Script to change between canvas.Yeah I know ...

Hi guys !

My first post here! i started coding/using unity about a 2 week ago (nerver code/use any programing language before) and i try to don’t use any help other than tutorial and api but am really stock here

I try to use button to show/hide canvas simple no ?..
For now it show the canvas but dont hide it.
Here’s my script so far :

public class  Buttonpanel : MonoBehaviour {
   
    public GameObject C1;
    public GameObject C2;
    public GameObject C3;
    public GameObject C4;
    public Button CC1;
    public Button CC2;
    public Button CC3 ;
    public Button CC4;



    void Start () {
       
        CC1 = CC1.GetComponent<Button> ();
        CC2 = CC2.GetComponent<Button> ();
        CC3 = CC3.GetComponent<Button> ();
        CC4 = CC4.GetComponent<Button> ();




    }

    public void C1show()
    {
        C1.SetActive (true);

    }

    public void C2show()
    {
        C2.SetActive (true);
       
    }

    public void C3show()
    {
        C3.SetActive (true);
       
    }

    public void C4show()
    {
        C4.SetActive (true);
       
    }

}


I know am missing something but ?

Thank you !

Hi,
You should have cX.SetActive(false) somewhere to turn it off,
Your code only have SetActive(true) which sets the canvas on every time.
It’s better to have a CXToggle() method doing something like cX.SetActive(!your_current_canvas_state)

1 Like