Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
0
Question by Stolorg · Mar 20, 2018 at 09:03 AM · eventsystemscrollbarlisteners

How to have one slider affect another slider's value?

So I have three sliders (in this case they are actually scrollbars, is slider and scrollbar really different?). I want the total value of all scrollbars = 1 (or 100%, whatever), so if S1 = .5, and S2 = .25, then S3 = .25, or .75/.25/0, etc... I want the other two scrollbars to adjust as the picked scrollbars moves, so if S1 is being slid, S2 and S3 increment or decrement their values as indicated by S1's value. I want this behavior to be consistent whether the player is moving S1, S2, or S3.

I thought that using the 'onValueChange' listener would do the trick, but my code keeps zeroing out the two non-selected sliders.

Here I set up the listeners:

 void Start()
 {
         maint.onValueChanged.AddListener(delegate { updateSliderValues(0); });
         res.onValueChanged.AddListener(delegate { updateSliderValues(1); });
         growth.onValueChanged.AddListener(delegate { updateSliderValues(2); });
 }

And here is the updateSliderValues method:

 private void updateSliderValues(int slider)
     {
         float total = maint.value + res.value + growth.value;
         float change = total - 1;
         float halfChange = change / 2;
 
         switch (slider)
         {
             case 0:
             {
                 Debug.Log("Slider " + slider + " value has changed to " + maint.value );
                 if (maint.value == 1)
                 {
                     res.value = 0;
                     growth.value = 0;
                 }
                 else
                 {
                     if (change > 0)
                     {
                         if (res.value >= halfChange && growth.value >= halfChange)
                         {
                             res.value -= halfChange;
                             growth.value -= halfChange;
                             Debug.Log("both minus");
                         }
                         else if (res.value < halfChange)
                         {
                             growth.value -= (change - res.value);
                             res.value = 0;
                             Debug.Log("growth minus");
                         }
                         else
                         {
                             res.value -= (change - growth.value);
                             growth.value = 0;
                             Debug.Log("res minus");
                         }
 
                     }
                     else
                     {
                         res.value = res.value + halfChange;
                         growth.value = growth.value + halfChange;
                     }
                 }
                 break;
             }
             case 1:
             {
                 Debug.Log("Slider " + slider + " value has changed to " + res.value);
                 if (res.value == 1)
                 {
                     maint.value = 0;
                     growth.value = 0;
                 }
                 else
                 {
                     if (change > 0)
                     {
                         if (maint.value >= halfChange && growth.value >= halfChange)
                         {
                             maint.value -= halfChange;
                             growth.value -= halfChange;
                         }
                         else if (maint.value < halfChange)
                         {
                             growth.value -= (change - maint.value);
                             maint.value = 0;
                         }
                         else
                         {
                             maint.value -= (change - growth.value);
                             growth.value = 0;
                         }
                     }
                     else
                     {
                         maint.value += halfChange;
                         growth.value += halfChange;
                     }
                 }
                 break;
             }
             case 2:
             {
                 Debug.Log("Slider " + slider + " value has changed to " + growth.value);
                 if (growth.value == 1)
                 {
                     maint.value = 0;
                     res.value = 0;
                 }
                 else
                 {
                     if (change > 0)
                     {
                         if (maint.value >= halfChange && res.value >= halfChange)
                         {
                             res.value -= halfChange;
                             growth.value -= halfChange;
                         }
                         else if (maint.value < halfChange)
                         {
                             res.value -= (change - maint.value);
                             maint.value = 0;
                         }
                         else
                         {
                             maint.value -= (change - res.value);
                             res.value = 0;
                         }
                     }
                     else
                     {
                         maint.value += halfChange;
                         res.value += halfChange;
                     }
                 }
                 break;
             }
         }
     }

I'm still learning, so perhaps I don't understand how 'onChangeValue' works? Or are they contradicting each other? I thought about using the event system and the onPointerUp event, but I have no idea how the events work or how to implement that. Any help would be appreciated!

Comment
Add comment · 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 Stolorg · Mar 20, 2018 at 04:42 AM 0
Share

I should add that it looks like my non-user-moved scrollbars are being zero'd out by some kind of loop, but I can't identify it.

0 Replies

· Add your reply
  • Sort: 

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

75 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

Related Questions

How do I find text on an object? 1 Answer

Having an issue blocking UI taps from passing through the UI into a gameobject. 3 Answers

OnPointerEnter works only if enter from blank space. 0 Answers

Alternative to EventSystem.current.currentSelectedGameObject 1 Answer

player jumps too high when clicking with two fingers. 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