• 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 /
  • Help Room /
avatar image
Question by Fragmental · Jun 19, 2018 at 04:20 PM · uiplayerprefseventtoggleplayerprefsx

Undesirable call of Toggle's "On Value Changed" when "isOn" is changed through script

I'm saving the user's value of "isOn" for various toggle states, and then setting those toggle states during start.

Each toggle has a method I want called when the user changes the value of the toggle, but not when the value is changed via script. However, it seems as though the "On Value Changed" event gets called when I change the value through script.

Here is a sample of the code that gets called in start to set the toggle

     private void Start()
     {  
         if (PlayerPrefs.HasKey("rememberAuth"))
         {
             rememberAuth = PlayerPrefsX.GetBool("rememberAuth");
             rememberToggle.isOn = rememberAuth;                
         }
         else
         {
             PlayerPrefsX.SetBool("rememberAuth", rememberAuth);
         }
     }

And here is a sample of the method that gets called by the "on value changed" event.

     public void RememberMeToggle()
     {
         if(rememberToggle.isOn)
         {
             SaveAuth();
         }
         else if (!rememberToggle.isOn)
         {
             DeleteAuth();
         }
         PlayerPrefsX.SetBool("rememberAuth", rememberAuth);
         PlayerPrefs.Save();
     }

Now, if the bool value of the toggle is different from the default value that was set in the editor, the RememberMeToggle() method gets called even though I don't want it to. T$$anonymous$$s can cause some issues.

I've found a solution for t$$anonymous$$s, and that is to remove the method call from the "On Value Changed" Event, in the inspector and then add an "Event Trigger" Component with a "Pointer Click" event and use that instead.

Is t$$anonymous$$s what I'm supposed to do? Is there a better option? Is the function of "On Value Changed" being trigger from the script desirable and intended?

I had a lot of trouble finding a solution through searches, so if I am doing t$$anonymous$$s right, even though I answered my own question, it might help someone else who's searc$$anonymous$$ng.

Comment
drebleezle

People who like this

1 Show 1
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 drebleezle · Apr 09, 2020 at 01:40 AM 0
Share

Thanks for posting this question - you were right, someone else was indeed searching!

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Hellium · Jun 19, 2018 at 05:05 PM

Here are two useful functions you can call to "turn off" and "turn on" the persistent listeners of a UnityEvent (such as toggle.onValueChanged)

 public void Mute( UnityEngine.Events.UnityEventBase ev )
 {
     int count = ev.GetPersistentEventCount();

     for ( int i = 0 ; i < count ; i++ )
     {
         ev.SetPersistentListenerState( i, UnityEngine.Events.UnityEventCallState.Off );
     }
 }
 
 public void Unmute( UnityEngine.Events.UnityEventBase ev )
 {
     int count = ev.GetPersistentEventCount();

     for ( int i = 0 ; i < count ; i++ )
     {
         ev.SetPersistentListenerState( i, UnityEngine.Events.UnityEventCallState.RuntimeOnly );
     }
 }


In your code:

   private void Start()
  {  
      if (PlayerPrefs.HasKey("rememberAuth"))
      {
          rememberAuth = PlayerPrefsX.GetBool("rememberAuth");
          Mute( rememberToggle.onValueChanged ) ;
          rememberToggle.isOn = rememberAuth;  
          Unmute( rememberToggle.onValueChanged ) ;              
      }
      else
      {
          PlayerPrefsX.SetBool("rememberAuth", rememberAuth);
      }
  }
Comment
Fragmental
drebleezle

People who like this

2 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 Fragmental · Jun 20, 2018 at 05:34 AM 0
Share

Thank you for your detailed response!

avatar image Fragmental · Jun 20, 2018 at 05:37 AM 0
Share

What are the for loops for in the mute/unmute methods?

avatar image Hellium Fragmental · Jun 20, 2018 at 06:06 AM 0
Share

The for loop retrieve each callback specified in the event, in the inspector (the persistent listener)

If the callbacks are attached using code, you will have to call toggle.onValueChanged.RemoveListener( function ) instead

avatar image Fragmental Hellium · Jun 20, 2018 at 06:49 AM 0
Share

Oh right. Of course. I forgot about adding multiple callbacks on the event. Thank you.

avatar image drebleezle · Apr 09, 2020 at 07:33 PM 0
Share

Thanks for the answer! Any other solutions since June 2018?

avatar image Hellium drebleezle · Apr 09, 2020 at 07:38 PM 0
Share

Doesn't the code works? I think the code is still valid today.

avatar image ramishaziev drebleezle · Apr 16, 2020 at 01:45 PM 1
Share

You can use Toggle.SetIsOnWithoutNotify if you want set isOn without invoking onValueChanged callback.

avatar image

Answer by Speedomon · Apr 11, 2022 at 05:03 PM

Fortunately there is an easier way now: SetIsOnWithoutNotify

Comment
Shaggy21
binglengdegnel

People who like this

2 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

208 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 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 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

How do I save the state of a toggle button? 1 Answer

UnityEvents for UI updating, generic way of removing repeated calls in same frame. 0 Answers

[Resolved] Image not changing on Toggle 1 Answer

How to save string arrays in Playerprefs 2017? 1 Answer

saving a text into a player pref 0 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