system
August 29, 2010, 1:31pm
1
I'd like to toggle a GUItexture to appear/disappear.
this code (below) i saw elsewhere on the forum hides and shows a mesh, but not a GUItexture.
any idea how to make the code hide a GUI textue. (Sorry im not a programmer.)
function Update() {
if (Input.GetKeyDown(KeyCode.Z)) {
// show
renderer.enabled = true;
}
if (Input.GetKeyDown(KeyCode.X)) {
// hide
renderer.enabled = false;
}
}
In the inspector create an empty gameobject. Name it GuiHere.
Create a new js script called MyGui.js and paste the following code
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), "Hello World!");
}
Add MyGui.js to the GuiHere.
Now create a new js script called Controller.js and paste the following code
var Guiscript : MyGui ;
function Update () {
if (Input.GetKeyDown ("space")){
if (Guiscript.enabled == true){
Guiscript.enabled = false;
}
else {
Guiscript.enabled = true;
}
}
}
Add the Controller.js to the Main camera. Add the GuiHere object in the slot of the Controller.js in the Main camera object.
Hit play. When you press the space bar, the gui appears. If you press it again, it disappears. By checking the drawcalls, you will see that it is completely disabled, not just not showing.
For further reading, I would suggest http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
and http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html
gameObject.guiTexture.enabled = false;
That might be deprecated?
gameObject.guiTexture.active = false :)
for GUI's id like to use
if (Input.GetKeyDown(KeyCode.Z)){
// show
Gameobject.guiText.enabled = true;
}
if (Input.GetKeyDown(KeyCode.X)) {
// hide
gameobject.guiText.enabled = false;
}