Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 jsonfan · Oct 28, 2015 at 01:18 PM · c#guislidersetters

Set Slider value but don't trigger On Value Changed

I have another scripts attached to On Value Changed and do not want to trigger them. Is this possible?

For example I want a toggle to reset all sliders to default values.

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

4 Replies

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

Answer by OncaLupe · Oct 28, 2015 at 05:42 PM

I had a similar issue with loading saved values for settings on game load. My solution was to have a bool set that the script looks for, and if set, ignores the change. So you could possibly do something like this:


public class SettingsHandler
{
  public static bool ignoreValueChanges;
  public void ResetValues()
  {
    ignoreValueChanges = true;
    slider1.value = slider1Default;
    slider2.value = slider2Default;
    ignoreValueChanges = false;
  }
}
public class OtherClass
{
  OnValueChange(float value)
  {
    if(SettingsHandler.ignoreValueChanges)
      return;
   //Other code here
  }
}
The OnValueChange code gets called immediately when the value changes, so the bool can be set back to false after all sliders/toggles/etc are changed. With a static bool each script won't need a reference and can just access it.
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 beppim · Oct 29, 2015 at 09:58 AM 0
Share

Are you sure that this works? I think that the ResetValues() is played atomically and the OnValueChange() is called either before, or after the ResetValue(), making the ignoreValueChanges variable useless. * Just for the pleasure of talking, I didn't try your code.

avatar image OncaLupe · Oct 29, 2015 at 03:48 PM 1
Share

@beppim I use basically the same thing when loading saved preferences in my game and setting sliders and toggles. Each OnValueChange() gets called immediately on setting the value for me.

If for some reason it doesn't work for someone, or they just want to be extra sure, you could do this ins$$anonymous$$d:


public void ResetValues()
{
  ...
  slider2.value = slider2Default;
  Invoke("ClearIgnoreBool", 0.1f);//Calls the method after 0.1 seconds
}
void ClearIgnoreBool()
{
  ignoreValueChanges = false;
}
avatar image Domvel · Nov 08, 2018 at 10:39 AM 0
Share

It works because the on change event is triggered directly like a interrupt. Before ignoreValueChanges is reset to false. $$anonymous$$aybe not 100% safe, but it works for now.

avatar image
1

Answer by beppim · Oct 28, 2015 at 05:55 PM

I think that the best thing to do is to create a boolean global flag that is checked by the listeners: if the last click was a reset, don't do anything. As soon as you move the slider you clear that boolean flag.

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
1

Answer by Yiming075 · Mar 03 at 12:13 PM

This works in Unity 2020.

 toggle.SetIsOnWithoutNotify(value);
 slider.SetValueWithoutNotify(value);
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 Lumpazy · Sep 15, 2018 at 10:12 AM

What worked for me and is maybe a bit less static : 1. delete the listener (the On Value Changed directive) in the Editor. 2. on Initialization use this code : myslider.value = someScriptableObject_Or_Initialization_Value; myslider.onValueChanged.AddListener( delegate { actionToPerformOnValueChanged(); } );

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

8 People are following this question.

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

Related Questions

how to control SMOOTHNESS via GUI slider?! It's not working...pls help! 1 Answer

How to draw slider thumb on top 0 Answers

Multiple Cars not working 1 Answer

OnGUI sliders only accept float? C# 2 Answers

Display actual number of bullets 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