Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 tomazsaraiva · Mar 19, 2010 at 11:10 AM · toggleswitch

Toggle GUI button

How does it work the GUI toggle?

I want to be able to switch between two textures if the toggle is on or off.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by runevision · Mar 19, 2010 at 11:21 AM

The GUI.Toggle is described on this page in the manual and here in the scripting reference.

If you want something to happen when toggling the toggle, here is one way to do it:

var toggleBool = true;

function OnGUI () { var toggleBoolNew = GUI.Toggle (Rect (25, 25, 100, 30), toggleBool, "Toggle");

 // Check if the toggle was toggled
 if (toggleBoolNew != toggleBool) {
     if (toggleBoolNew == true)
         Debug.Log("Toggle was enabled");
     else
         Debug.Log("Toggle was disabled");

     toggleBool = toggleBoolNew;
 }

}

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 tomazsaraiva · Mar 19, 2010 at 12:16 PM 0
Share

Thanks..now I understand how to activate functions regarding the on/off state of the toggle

avatar image Lipis · Mar 19, 2010 at 12:34 PM 0
Share

== true could be omitted and consider adding the curly brackets {} even if you have only one line to execute.. because usually you don't have only one.. for example.. what if you want to add the code for changing the texture and see Debug.Log("Texture was changes")..

avatar image Lipis · Mar 19, 2010 at 12:35 PM 0
Share

@maveryck21 don't forget to upvote awesome answers.. especially the ones that helped you :)

avatar image runevision ♦♦ · Mar 19, 2010 at 01:57 PM 0
Share

@Lipis I added == true to make the code more readable - this language construct is not obvious for people new to program$$anonymous$$g, for example. But you're right that it can be left out. About where to use curlies, that is a matter of taste.

avatar image beco13 · Aug 02, 2011 at 02:50 AM 0
Share

@Rune Skovbo Johansen ♦♦

what if I only want to play a sound once using toggle button??because I tried your code and the sound keeps playing...

How do I make it done once??

PROBLE$$anonymous$$ SOLVED : by adding a conditional statement before setting the boolean to true I'm able to set the toggle to play the sound only once.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Switching between set of components 1 Answer

Toggle Switch 6 Answers

How to turn on a light, then off with the same button. 1 Answer

Switch between mouse controller character to controlling camera? 0 Answers

Toggle Scripts On & Off 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