Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Daniel-Talis · Apr 30, 2012 at 03:38 AM · guitoggle

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;
        
         }
  }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · Apr 30, 2012 at 03:56 AM 0
Share

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()

avatar image Daniel-Talis · Apr 30, 2012 at 04:02 AM 0
Share

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?

avatar image Lo0NuhtiK · Apr 30, 2012 at 07:14 AM 0
Share

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?

avatar image Daniel-Talis · Apr 30, 2012 at 07:43 AM 0
Share

I'll try with a new scene..

avatar image Daniel-Talis · Apr 30, 2012 at 08:18 AM 0
Share

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.

Show more comments
avatar image
0

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 ;
        }
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · Apr 30, 2012 at 08:59 AM 0
Share

close... I'll edit my post

avatar image Daniel-Talis · Apr 30, 2012 at 09:05 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GUI Button selected changes color and stays until another button is clicked 1 Answer

Close a GUI.Toggle when another opens in a different script? 0 Answers

Neutral state for toggle buttons? 2 Answers

GUI window popup button 1 Answer

Little help with GUI Toggle needed 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges