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 yosss · Jun 07, 2010 at 01:28 PM · guibuttontoggle

Need help with toggle button

First of all sorry for posting the whole code here but if there is a chance that anyone of you would like to improve it overall may it be so. Now on to my problem. The thing is that the toggle buttons wont toggle and their values are always set to zero (the togglewata and togglebloom ones) so here is the code

var togglewata : boolean = true; var togglebloom : boolean = true; var optionsobject : GameObject; var togglewaterbool : boolean = true; var togglebloombool : boolean = true; var isinoptions = 0; var isingame = 0; function Awake(){ isinoptions=0; isingame = 0; }

function OnGUI () { //MAIN MENU if(isinoptions==0 ){ GUI.Box (Rect (10,10,100,90), "Loader Menu");

if (GUI.Button (Rect (105,40,110,20), "Load World")) { isinoptions=2; Application.LoadLevel ("world"); }

 if (GUI.Button (Rect (105,70,110,20), "Lag-Free World")) {
     Application.LoadLevel ("Waterless World");
 }

if (GUI.Button (Rect (105,100,110,20), "Options")) { isinoptions=1; } if (GUI.Button (Rect (105,140,110,20), "Quit")) { Application.Quit(); }

} //OPTIONS MENU if(isinoptions==1){

togglewata = GUI.Toggle (Rect (25, 25, 100, 30), optionsobject.GetComponent("togglewaterbool"), "Water"); togglebloom = GUI.Toggle (Rect (25, 50, 100, 30), optionsobject.GetComponent("togglebloombool"), "Bloom"); if (GUI.Button (Rect (105,140,90,20), "Back")) { isinoptions = 0; }

}

}

function Update () { if (Input.GetKeyDown(KeyCode.P)) Application.CaptureScreenshot("Screenshot.png"); }

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
Best Answer

Answer by Cyclops · Jun 07, 2010 at 02:00 PM

Personally, I think it's better to store the toggle values in a separate array. If you don't mind coding it that way, here's another Answer that has full code: How can I setup a series of toggle buttons?

And another thing I noticed, is that you're not giving the saved toggle value to the Toggle button code (again, see the other Answer for an example of how it's done), so the button will never change. You have to tell the Toggle function, what value to use every time you call it. For example, I think this should do it (haven't tested it):

hasWater = GUI.Toggle (Rect (25, 25, 100, 30), hasWater, "Water");

Update: minor point, but to follow good naming practices, I'd rename the Booleans, as above for example, to hasX.

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 yosss · Jun 07, 2010 at 02:12 PM 0
Share

aha and should i replace togglewata with optionsobject.GetComponent("togglewaterbool") because thats where i want to store my values

avatar image yosss · Jun 07, 2010 at 02:18 PM 0
Share

okay your code needed a little tweaking and it was good to go thanks a lot

avatar image Cyclops · Jun 07, 2010 at 02:32 PM 0
Share

@yosss, hm, yeah, I didn't really think about it, but you do have two Booleans that are almost identical, you really only need one per value. And no, there shouldn't be a GetComponent at all - just use the Boolean value in the Toggle call, and assign the return value back to the Boolean.

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

No one has followed this question yet.

Related Questions

GUI window help 1 Answer

Whats wrong with my GUI.Toggle? 2 Answers

Is there a better way to access the single active Toggle in a ToggleGroup? 4 Answers

GUI window popup button 1 Answer

Toggling multiple button states 2 Answers


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