- Home /
Question by
Ali_Jaffer · Sep 20, 2014 at 03:06 PM ·
canvas
How to enable and disable a canvas window by scripting
Hi,
I want to ask a question about UGUI. that i want to add a canvas window with four buttons at run time. how to do it please help. Please help me
Comment
Best Answer
Answer by TrollHachem · Sep 20, 2014 at 03:42 PM
Just like you disable , any gameobject? make a variable , assign the Canvas then simply ,
void DisableCanvas() {
CanvasObject.SetActive(false);
}
TrollHachem's comment isn't quite right, which is why you defaulted to canvas.enabled = true;.
What he meant to say was canvas.gameObject.SetActive(true);.
Answer by Tulsisvt · Feb 25, 2016 at 08:39 AM
The above answer is correct if you want to disable the gameObject itself. To disable the Canvas component you can use this:
CanvasObject.GetComponent<Canvas> ().enabled = false;
This avoids null object errors.
Btw, this is the Unity recommended option if you want optimal performance.