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
1
Question by Noxury · Oct 16, 2016 at 11:32 PM · playerprefstoggleboolinitializationssao

Toggle OnValueChanged ignore on start

So I have an UI.Toggle on my video settings menu that toggles a SSAO-bool and therefore enables/disables the SSAO script. I dropped the ToggleSSAO() method on OnValueChanged-field, so it gets called, when its bool "isOn" has changed.

 public void ToggleSSAO(){
     ssao = !ssao;
     GetComponent<UnityStandardAssets.ImageEffects.ScreenSpaceAmbientOcclusion>().enabled = ssao;
     BoolPrefs.SetBool("SSAO", ssao);
 }

I have used Eric's BoolPrefs script that is simulating bool values to save them as ints in the PlayerPrefs class.

Anyways, at start of the game, the saved ssao-bool should initialize the ssao bool itself and the isOn bool on the UI.Toggle.

 void Awake() {
     ssao = BoolPrefs.GetBool("SSAO", true);
     GameObject.Find("Toggle_SSAO").GetComponent<Toggle>().isOn = ssao;
 }

The problem is that OnValueChanged() therefore gets called at the beginning of the game because of this initialization and switches the ssao-bool again, so the isOn bool is inverted. How can I resolve 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ElijahShadbolt · Oct 17, 2016 at 04:43 AM

Change the method to look like this:

 public void ToggleSSAO(bool newValue) {
     ssao = newValue;
     //GetComponent...
 }

Drop the Dynamic bool ToggleSSAO method in the OnValueChanged event instead of the Static Parameters ToggleSSAO. You should see no check box in the event. This way whenever the Toggle's isOn is changed, it will also set ssao.

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
avatar image
0

Answer by Wiechering · Mar 06, 2020 at 11:08 AM

This is very old, which i know but somehow after googling another topic i ended up here. For anyone else having the described problem:

Just call Toggle.SetIsOnWithoutNotify( state );

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 Bunny83 · Mar 06, 2020 at 01:32 PM 0
Share

This can be used but it still wouldn't make much sense as such kind of program$$anonymous$$g is dangerous and error prone. $$anonymous$$eeping track ot the same state in two different places simultaneously just makes no sense. If you use a toggle (which has an internal state) it should represent your setting and you just apply that state to the thing you actually want to set. Blindly toggling whenever a change happens just makes no sense.

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

88 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

playerprefs dont load for menu toggle 0 Answers

Toggle bool with keypress -2 Answers

bool field initialization ignored 0 Answers

Save a Toggle Group state? 0 Answers

Using an Enum for GUI.Toggle? 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