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 RichCoggin · Jul 19, 2013 at 12:10 PM · javascriptguibuttontoggle

GUI.Toggle to only send one action, not repeated

Hi people,

I've set up a script with a number of toggle buttons. I have them switching on and off when clicked which is fine. When clicked, a function is performed, but it keeps sending the message. What I want to do is just send a single message/action once. This is obviously a standard button action, but I couldn't figure out how a button could have an on/off state; change graphic when touched.

Here's my code:

 #pragma strict
 
 var LeftNavSkin: GUISkin;
 //var toggle = true;
 
 var bt1 = false;
 var bt2 = false;
 var bt3 = false;
  
 // Clears everybody and returns true to help setting the desired one
 function setMeOnly():boolean{
  
   bt1 = bt2 = bt3 = false;
   return true;
 }
  
 function OnGUI () {
  
   GUI.skin = LeftNavSkin;
   
   GUI.Box (Rect (-30,10,100,90), "");
  
   //Top Left Button
   if (GUI.Toggle (Rect(50,300,100,100), bt1, "")) bt1 = setMeOnly();
   
  
   //Middle Button
   if (GUI.Toggle (Rect(50,410,100,100), bt2, "")) bt2 = setMeOnly();
  
   //Bottom Right Button
   if (GUI.Toggle (Rect(50,520,100,100), bt3, "")) bt3 = setMeOnly();
         
 //This sends the debug.log repeatedly. I want it to just send once.
 
 if(bt1 === true)
 {
    Debug.Log("OMG IT WORKS");
 }
 
 }

Help would be appreciated!

Rich

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
0

Answer by Jamora · Jul 19, 2013 at 12:38 PM

If you only ever have one button on at one time, you can use a SelectionGrid with a custom style. Define your Style in a GUISkin. You can set the different textures for when the button is pressed/hovered over etc. via the inspector.

As for your function problem, after the SelectionGrid, use if(GUI.changed) MyFunction(); Alternatively keep two ints, one for the old value, one for the new and then check each frame if the old value matches the new one.

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 RichCoggin · Jul 19, 2013 at 01:13 PM 0
Share

Thanks. That is the kinda thing I'm looking for. Can you tell me how you arrange the grid differently. It's 4 up at the moment. I would like to go 4 in a line down for example.

Thanks again

avatar image RichCoggin · Jul 19, 2013 at 02:14 PM 0
Share

Ok, done it. Thanks a bunch.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way of using just basic gui.Buttons as toggles? 1 Answer

GUI Toggle Using Button Style Remain Active 1 Answer

GUI Button which looks like a Toggle 0 Answers

Scripting a GUI button to move object smoothly 1 Answer

Trouble getting GUI phone to open/close 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