Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 sphase · Apr 17, 2016 at 12:21 PM · c#uisliderincrementdynamic variables

Problem with UI slider to make it increment other variables

Hi there, I'm relatively new to using Unity and I'm currently working on a simple 2d menu which involves a horizontal slider and buttons. Right now, I'm trying to increment an integer variable by 1 when the handle bar of the slider is dragged to the sliders max value. I've tried a few different things and it won't seem to increment by 1, instead of incrementing by 1; the variable increments by a random number such as 1, 5, 6.

I'm doing the code with C# and using a dynamic float function to control the slider. When the slider reaches the max value, I want the handle bar to go straight back to the lowest value and that works fine, but once the slider moves back to the min value, this is when I want the i variable to increment by 1 but it increments by a random number instead.

my logic looks like this:

   public Slider mainSlider;
   public float sliderVal = 0f;
   bool validIncrement = false;
   int i = 0;
    
    public void setSlider(float slider)
     {
         sliderVal = slider;
 
         if (sliderVal == mainSlider.maxValue)
         {      
             mainSlider.normalizedValue = mainSlider.minValue;
         }
 
     
         if(sliderVal == mainSlider.minValue)
         {
             validIncrement = true;
         }
         else
         {
             validIncrement = false;
         }
     }
 
   
 
     public void submitValue()
     {
         Debug.Log("i: " + i);
 
 
         if (validIncrement)
         {
             i++;
         }
     }

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
Best Answer

Answer by MrDiVolo · Apr 17, 2016 at 03:35 PM

Hi there, I'll try to explain everything as simply as I can, but if there's anything you don't understand just ask and I'll update this answer with more detail.


Random Incrementing

When your mouse is over the end of the slider, and your mouse is pressed, Unity tries to move the slider to the max value (as you don't need to press on the handle of the slider itself to move it).


My Solution

Set up your slider as I've done below; the only things I've changed from the default is the slider only uses Whole Numbers, and the min and max values are 0 and 1 respectively. Then in the OnValueChange section of the slider, I've used itself as the object, and selected the method OnValueChange of my IncrementSlider script.

alt text


Now I'll walk through the script;

  1. I can see from your above solution that you are using public variables; if you don't need this variable to be accessed by other scripts, you can make the variable private, and instead use the Attribute SerializeField, to make it visible in the inspector. This variable I have set to the Slider that the script is attached to, but dragging it in the inspector.

  2. The m_myValue variable is your public int i variable

  3. The boolean m_increment, is what I'm using to allow the m_myValue variable to be incremented once the slider returns to the minimum value.

  4. The OnValueChange method, checks if the slider is being slid to the maximum value, and sets the m_increment variable to true

  5. In the Update method, we check if the mouse button 0 (left mouse button) has been released, and if the m_increment boolean is true. If these are both true, the slider's current value will be set to the minimum value, and the m_myValue variable will be incremented.

      public class IncrementalSlider : MonoBehaviour
      {
         [SerializeField]
         private Slider  m_slider;
         private int     m_myValue;
         private bool    m_increment;
     
         public void Update()
         {
             if (Input.GetMouseButtonUp(0) && m_increment)
             {
                 m_slider.value = m_slider.minValue;
                 m_myValue++;
             }
         }
     
         public void OnValueChange()
         {
             if (m_slider.value == m_slider.maxValue)
                 m_increment = true;
         }
     }
    
    

    Round Up

This solution doesn't have a way to 'cancel' the action, but if you wanted one, then simply add an else section to the OnValueChange if statement, to set the m_increment bool to false.

Tip; if you want to see the values of private variables without the SerializeField attribute, you can select the debug mode for the inspector.

I hope this helped you.


incrementalslider.png (27.4 kB)
Comment
Add comment · Show 1 · 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 sphase · Apr 17, 2016 at 04:02 PM 0
Share

makes sense. I never had thought about the action of the left clicking on the mouse over the handle bar. Thanks for the helpful solution!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How do I move my train with a slider, and not W & S 1 Answer

How to get float value based on the element size? 0 Answers

Saving a UI Text Number Multiple Times 2 Answers

PlayerPrefs not saving slider value & audio 2 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