- Home /
Show & Hide GUItexture on keypress
- 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; }
}
this script is quite a sexy answer: http://www.unifycommunity.com/wiki/index.php?title=FadeIn
Answer by Ippokratis · Aug 29, 2010 at 02:58 PM
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
This works great, but how do you change it from the Hello World text to an actual texture?
and also when i click play the texture is already there..
A cleaner version:
if (Input.Get$$anonymous$$eyDown ("space")){
Guiscript.enabled = !Guiscript.enabled;
}
Answer by jtbentley · Aug 30, 2010 at 03:02 PM
gameObject.guiTexture.enabled = false;
That might be deprecated?
gameObject.guiTexture.active = false :)
Thanks for bringing this to my attention. It might work both ways, haven't tested your suggestion though. The method you suggest should deactivate the gameObject (gameObjects are active/inactive) while the method I suggest deactivates the $$anonymous$$onoBehaviour (enabled/disabled). I think that since ot is here http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.html it is not depreciated. Anyway, I didn't knew the active / inactive variable until now, so thanks.
Answer by M Poirot · Apr 30, 2011 at 07:55 PM
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; }
Your answer
Follow this Question
Related Questions
Show/hide GUI texture 2 Answers
on collision, show/hide other model scripting ? 1 Answer
Hide GUI Text when button is pressed 1 Answer
hide show canvas and ui elements at start 1 Answer
hide and show node for turret places 1 Answer