• 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 Dozy- · May 31, 2019 at 07:44 AM · updateslidermathf.clamp

How do you make slider children equally decrease when slider parent decreases?

Hi, so I'm trying to make a "muffin boxes slider" and 4 "customer sliders." Each muffin box has 4 muffins. If the muffin box slider increases by 1, all 4 customers have their sliders increased by 1. If the slider decreases by 1, each customer slider decreases by 1. If I have 2 muffin boxes, I can distribute 8 muffins among the customers. alt text

I have 2 scripts: one for clamping the customers' muffins to how many muffins are available and another to increase/decrease the available muffins.


Everything works fine until I try to decrease the available muffins (lowering the boxes slider). Instead of all customers' muffins decreasing equally, Amy seems to decrease more than the rest. eg. there are 2 boxes (8 muffins) and Amy and Bob start with 4 muffins while the other 2 have zero. If I reduce boxes to 1 (4 muffins), Amy goes down to 0, Bob to 3, and remain 1. I'd like the decrease to be Amy and Bob at 2. I think it's some coding order because all decreases start with Amy. Here is the box code:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class boxScript : MonoBehaviour
 {
     public Slider boxSlider, slider01, slider02, slider03, slider04;
 
     public void onBoxChange()
     {
         float muffins = boxSlider.value * 4f; //1 box has 4 muffins
         float customerTotal = slider01.value + slider02.value + slider03.value + slider04.value;
         float remain = muffins - customerTotal;
         float addValue = remain / 4f; //make customers inc/dec based on box
 
         slider01.value = slider01.value + addValue;
         slider02.value = slider02.value + addValue;
         slider03.value = slider03.value + addValue;
         slider04.value = slider04.value + addValue;
 
     }
 }

Here is the clamp code:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class clampScript : MonoBehaviour
 {
     public Slider boxSlider, slider01, slider02, slider03;
     public Slider thisSlider;
 
     public void onCustomerChange()
     {
         float muffins = boxSlider.value * 4f;
         float customerTotal = thisSlider.value + slider01.value + slider02.value + slider03.value;
         float remain = muffins - customerTotal;
 
         if (remain > 0)
         {
             thisSlider.value = Mathf.Clamp(thisSlider.value, 0f, (muffins - (slider01.value + slider02.value + slider03.value)) + remain);  //say 4 muffins: slider123 have 1 ea. this has 0; remain = 1. thisMax: 4-3=1                                            
             slider01.value = Mathf.Clamp(slider01.value, 0f, (muffins - (slider02.value + slider03.value + thisSlider.value)) + remain); //4 muffins: slider123 have 1 ea. this has 0; remain = 1. slilder123Max = 4-2=2
             slider02.value = Mathf.Clamp(slider02.value, 0f, (muffins - (slider03.value + slider01.value + thisSlider.value)) + remain);
             slider03.value = Mathf.Clamp(slider03.value, 0f, (muffins - (slider01.value + slider02.value + thisSlider.value)) + remain);
         }
         else
         {
             thisSlider.value = Mathf.Clamp(thisSlider.value, 0f, muffins - (slider01.value + slider02.value + slider03.value));  //say 4 muffins: slider123 have 1 ea. this has 0; remain = 1. thisMax: 4-3=1                                            
             slider01.value = Mathf.Clamp(slider01.value, 0f, muffins - (slider02.value + slider03.value + thisSlider.value)); //4 muffins: slider123 have 1 ea. this has 0; remain = 1. slilder123Max = 4-2=2
             slider02.value = Mathf.Clamp(slider02.value, 0f, muffins - (slider03.value + slider01.value + thisSlider.value));
             slider03.value = Mathf.Clamp(slider03.value, 0f, muffins - (slider01.value + slider02.value + thisSlider.value));
         }
 
     }
 
 }

Each customer Slider has the clamp code with a 'on value change' to itself & onCustomerChange(). The box has the box code and has 'on value change' to itself & onBoxChange().

muffins.png (22.7 kB)
Comment
Add comment · Show 2
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 MadDevil · May 30, 2019 at 02:51 AM 0
Share

if(remain > 0) { "Do your thing" } else { "don't" }

avatar image Dozy- MadDevil · May 30, 2019 at 01:38 PM 0
Share

Thank you very much @$$anonymous$$adDevil for replying! I used if/else in the updated code

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.

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

110 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

Related Questions

Is there any difference between mobile and desktop physics? 1 Answer

transform.LookAt not updating each frame until UI Slider input is changed 1 Answer

Update slider every frame with value from another script c# 1 Answer

(Logic) Switch GameObjects With Slider Using Arrays 0 Answers

Customize slider in Shader 3 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