Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by nedan · Jun 28, 2020 at 03:23 PM · script.slideruser interface

How to lock slider increase?

I have two sliders. and what I want to happen is that when value of both of the sliders is equal to 50 then I want player to not be able to increase slider value further, however still be able to decrease the value. another thing I think I would have to do is to make one of slider value decrease to 50 when value of both sliders is higher than 50.

 public class SliderLock : MonoBehaviour
 {
     public Slider AssasinLock;
     public Slider DestroyerLock;
     public float LockNumber = 50;
     public float sum;
 
     // use this initialization
     void Start()
     {       
     }
 
     // Update is called once per frame
     public void Update()
     {
         sum = AssasinLock.value + DestroyerLock.value;
         if(LockNumber <= sum)
         {
             DestroyerLock.interactable = false;
             AssasinLock.interactable = false;
         }
         else
         {
             DestroyerLock.interactable = true;
             AssasinLock.interactable = true;
         }
     }
 }        


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Jun 28, 2020 at 06:21 PM

@nedan, don't do it in your update loop - it is inefficient to waste update cycles on that. The below uses Unity Events on the sliders components, which are only called when the slider changes.

That said, This is kind of a long way around, and not the most efficient code, but it works.

The basic idea is to keep a separate max value for each slider, and adjust them upward or downward when the other slider is moved.

The following can be dropped on any game object, and then plug in the sliders to the inspector fields. The other two variables can be made private, but are there for you to watch the max values for each slider if you like.

The code assumes you are working with integer values (check the slider to use Whole Numbers checkbox), and you've set the maximums to 100 for each slider.

Then set up a Unity Event on each slider, drop the game object that owns the below script on Event object field, and then choose the appropriate Dynamic Float function name (e.g. SetSlider1Value ) from the function drop down in the Unity event for each slider.

 public class SetSliders : MonoBehaviour
 {
     public Slider slider1;
     public Slider slider2;


     public int totalValue = 100;
     public int[] sliderMaxes;
 
     public void Start() {
         sliderMaxes = new int[]{ 100, 100};
     }
 
     public void SetSlider1Value( System.Single currentValue ) {
         LimitSlider( slider1, currentValue, 1, 2 );
     }
 
     public void SetSlider2Value( System.Single currentValue) {
         LimitSlider( slider2, currentValue, 2, 1 );
     }
 
     public void LimitSlider( Slider aSlider, float currentValue, int thisIndex, int otherIndex ) {
         int current = Mathf.CeilToInt( currentValue );
         if( current > sliderMaxes[thisIndex - 1] ) aSlider.value = sliderMaxes[thisIndex - 1];
         sliderMaxes[otherIndex - 1] = totalValue - current;
     }
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

238 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 to make or find advanced UI assets 1 Answer

link GUI slider to shader opacity 0 Answers

Make 2 sliders dependent on each other 1 Answer

Slider isn't moving smoothly. (Unity3d) 1 Answer

Does anyone know why this script has a strange icon? 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