- Home /
GuiTexture toggle on keypress.
Hi there, I have a scene with multiple GuiTextures in it. With a keypress they toggle on and off. Initially this works but my problem is that the gui textures are turned on and off individually during the game making the code not work. I need to be able to toggle all GuiTextures on and off at the same time with a keypress.... Can anyone help? Here is the code I have on each GuiTexture.
guiTexture.enabled = false;
function Update() {
if (Input.GetKeyDown(KeyCode.F3)) {
guiTexture.enabled = !guiTexture.enabled;
}
}
Answer by Seth-Bergman · Apr 30, 2012 at 03:47 AM
if you want them all synchronous, try using a variable:
In One script as a controller:
static var guiOn : boolean;
if(Input.GetKeyDown(KeyCode.F3))
guiOn = !guiOn;
in the script attached to the texture objects:
if(controllerScript.guiOn)
guiTexture.enabled = true;
else
guiTexture.enabled = false;
I'd rather do it with something like this, and only attach this one script to my camera or the player or something ->
var gTxture : GUITexture[] ;
function Update(){
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F3))
ToggleTextures() ;
}
function ToggleTextures(){
gTxture = GameObject.FindObjectsOfType(GUITexture) ;
for(var i : int = 0 ; i < gTxture.length ; i++){
gTxture[i].guiTexture.enabled = !gTxture[i].guiTexture.enabled ;
}
}
...and if you're not planning on making any more guiTextures other than what you have at the beginning, then be better off to put the line [gTxture = GameObject.FindObjectsOfType(GUITexture) ;] in Start() ins$$anonymous$$d of the ToggleTextures()
Unfortunately I haven't had success with either of these examples. Lo0Nuhti$$anonymous$$.. I put your code on the camera and it toggles some but not all GuiTextures. Seth Bergman..I made an empty object for the first part and put the second part on all GuiTextures. No errors but no effect either. I think I need to see a working project to see this in action, can anyone supply one?
It should toggle all of them.
EDIT:
I just now loaded a scene with like 15-20 GUITextures and placed the code above in a test script... it works.
You sure some of the things that aren't working on yours aren't some other type of GUI element? Like GUIText or something?
Ok here are my findings. Your code works perfectly until I change the status of a GuiTexture. I made a new scene with three GuiTextures. On one I put this code..
var list_4 : GUITexture; var list_12 : GUITexture;
function On$$anonymous$$ouseUp(){ list_4.enabled = false; list_12.enabled = true; }
This toggles one of the textures. Now when I try your code to turn off all of the GuiTextures, it misses the one that was changed.
Answer by Daniel-Talis · Apr 30, 2012 at 08:59 AM
Do you mean like this..?
var allOff : boolean = false ;
var gTxture : GUITexture[] ;
function Update(){
if(Input.GetKeyDown(KeyCode.F3))
ToggleTextures() ;
}
function ToggleTextures(){
gTxture = GameObject.FindObjectsOfType(GUITexture) ;
for(var i : int = 0 ; i < gTxture.length ; i++){
gTxture[i].guiTexture.enabled = !gTxture[i].guiTexture.enabled ;
alloff = !allOff ;
}
}
Just tried it and success. $$anonymous$$uch appreciated. I know what you mean about taking naps to clear the $$anonymous$$d, I do it myself... Thanks kindly.