Hi, I have a GUITexture in my scene and I am wanting to toggle it on and off. Here is my current code which gives no errors and will hide the Gui but will not show it again. Why is this and how can I get it to toggle?
gameObject.active = true;
function Update() {
if (Input.GetKeyDown(KeyCode.F1)) {
gameObject.active = !gameObject.active;
}
}
I believe the problem here is this…
The first time it will succeed in deactivating the gameObject. However, by deactivating the gameObject, you’ve effectively stopped the gameObject from running it’s Update() function. It will never check again whether it’s active or not. I think a better alternative for you will be to, instead of activating and deactivating the actual gameObject, try something like…
function Update() {
if (Input.GetKeyDown(KeyCode.F1)) {
guiTexture.enabled = !guiTexture.enabled;
}
}
That way, the GameObject has not been deactivated and can still check to see if the button was pressed. Hope this solves it.