- Home /
How to set the 'enabled' bool of a Canvas GameObject in JS
(Unity 4.6 beta 17) I have a GameObject in the hierarchy that is a UI canvas and I want to be able to enable/disable it with the press of a key.
I want it to do the exact same thing as when you make a UI button, go to the "On Click ()" option in the Inspector and choose the canvas, then for the function choose "Canvas" > "bool enabled".
Please don't be vague and give me an example script like how to get the game object (correctly to get the canvas) and how to set the boolean.
Answer by Eric5h5 · Sep 20, 2014 at 09:03 PM
var myGameObject : GameObject;
var canvasEnabled : boolean;
function Start () {
myGameObject.GetComponent(Canvas).enabled = canvasEnabled;
}
Change canvasEnabled to the opposite value and do the GetComponent.enabled thing again.
Can I have an example please? I'm having trouble trying to toggle it.
myGameObject.GetComponent(Canvas).enabled = !myGameObject.GetComponent(Canvas).enabled
Your answer
Follow this Question
Related Questions
Unity 4.6 CanvasScaler: access "Match Width or Height" ratio from UnityScript? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Changing the light color with sliders 1 Answer
Scripted UI.text not shows on duplicated object 0 Answers
UI.Text: Lerping scores in Update -- excessive String allocation? 0 Answers