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 melonman · Mar 01, 2012 at 11:55 AM · guitoggle

Gui toggle active inactive states

Hi im trying to create a GUI button that controls if an object is active or not.

So far I have done this using the following code but unity is calculating these lines so fast that the object will flick betweeen active and inactive.

             if (GUI.Button (new Rect(Screen.width/2 - GUI2_button1_width/2 - GUI2_button1_flatspace, Screen.height/2 - GUI2_button1_height/2 - GUI2_button1_vertspace, GUI2_button1_width, GUI2_button1_height), "Soil type survey") && Soil_Survey.active == false) 
             {
             print ("You clicked the button!");
              Soil_Survey.active = true;
             }
             else if(Soil_Survey.active == true)
             {
             Soil_Survey.active = false;
             }


How do overcome this?

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

3 Replies

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

Answer by melonman · Mar 01, 2012 at 02:14 PM

I figured it out your code is pretty much spot on except I needed to take out the last part of the code in the top line which states "&& Soil_Survey.active == false"

So the final revised piece which works and your quite right is......

 if (GUI.Button (new Rect(Screen.width/2 - GUI2_button1_width/2 - GUI2_button1_flatspace, Screen.height/2 - GUI2_button1_height/2 - GUI2_button1_vertspace, GUI2_button1_width, GUI2_button1_height), "Soil type survey"))
 
                  {
                       if(Soil_Survey.active == false)
                       {
                     Soil_Survey.active = true;
                       }
                     else if(Soil_Survey.active == true)
                       {
                     Soil_Survey.active = false;
                       }
                       
                  }
Comment
Add comment · Show 1 · 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 melonman · Mar 01, 2012 at 02:17 PM 0
Share

Haha I guess I was a lil late in posting this.

Yeah I just seen that aswell sorry guys

and thank you both for your speedy replies.

$$anonymous$$ Franken I just tried to click the tick on your post and it wouldn't let me tick two so apologies :(

avatar image
0

Answer by Dreamside · Mar 01, 2012 at 12:15 PM

after assigning Soil_Survey=true; statement you check

else if(Soil_Survey.active==true) { Soil_Survey.active=false; }

Soil_Survey.active is always toggle fast because you assign it to true,and then check if it is true.If true you assign it to false.After clicking it becomes true and immediately again false.

What you should do is:

 if (GUI.Button (new Rect(Screen.width/2 - GUI2_button1_width/2 - GUI2_button1_flatspace, Screen.height/2 - GUI2_button1_height/2 - GUI2_button1_vertspace, GUI2_button1_width, GUI2_button1_height), "Soil type survey") && Soil_Survey.active == false) 
          {
           if(Soil_Survey.active==true)
           {
             Soil_Survey.active=false;
           }
           else
           {
             Soil_Survey.active=true;
           }
          }
Comment
Add comment · Show 3 · 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 melonman · Mar 01, 2012 at 01:42 PM 0
Share

Hey ebyaz,

Tried it and I see what you've done soon as I saw it I was pretty damn sure it would work but still no luck.

Now the object stays on screen but if I click the GUI button again the object doesn't disappear?

avatar image Tommynator · Mar 01, 2012 at 01:50 PM 0
Share

Remove the last condition in the end of you if statement "&& Soil_Survey.active == false". ebyaz forgot to remove it in his answer.

avatar image Dreamside · Mar 01, 2012 at 01:58 PM 0
Share

my bad, it was too long and i don't see that.$$anonymous$$ is absolutely right.

avatar image
0

Answer by Tommynator · Mar 01, 2012 at 01:52 PM

 if (GUI.Button (new Rect(Screen.width/2 - GUI2_button1_width/2 - GUI2_button1_flatspace, Screen.height/2 - GUI2_button1_height/2 - GUI2_button1_vertspace, GUI2_button1_width, GUI2_button1_height), "Soil type survey")) 
 {
     Soil_Survey.active = !Soil_Survey.active;
 }
Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Whats wrong with my GUI.Toggle? 2 Answers

Is there a multiple selection ugui toggle group? 1 Answer

why doesnt my script turn off on toggle? 1 Answer

GUI toggle on click, unless on GUI. 1 Answer

how to make a Horizontal slider & toggle in C# with GUI textures 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