Search Unity

Unity 4.6 - Is it possible to simply feed a public float into a button/slider/scrollbar?

Discussion in 'UGUI & TextMesh Pro' started by AwesomeX, Aug 20, 2014.

  1. AwesomeX

    AwesomeX

    Joined:
    Sep 10, 2013
    Posts:
    117
    Loving the new 4.6 Beta, but have a quick question regarding how events/On Value Changed thing works for the new UI tools.

    I'm trying to control a player's speed *a public float variable*, but it's not showing on the drop-down menu?

    I ended up having to feed it through a function, but it's an extra step which is a bit odd. Maybe I'm doing it wrong, could someone post the correct way on doing this?

    Thanks.
     
  2. AwesomeX

    AwesomeX

    Joined:
    Sep 10, 2013
    Posts:
    117
    Anyone?
     
  3. Aurore

    Aurore

    Director of Real-Time Learning

    Joined:
    Aug 1, 2012
    Posts:
    3,106
    Wrong forum, moved to developer preview.
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Use properties.
     
    Melang likes this.
  5. deram_scholzara

    deram_scholzara

    Joined:
    Aug 26, 2005
    Posts:
    1,043
    If you add an event to the OnValueChanged() section of the Slider component, you'll be able to drop the object with your custom component (the one with the player speed variable) onto the left-most slot of that event. Then you can select MyCustomComponet > myFloatSpeedVariable from the dropdown.

    Alternatively you could create and reference a function that would take the slider's normalized value and turn it into a more direct player speed value... like this:
    Code (CSharp):
    1. void ChangePlayerSpeed (float normalizedSpeed) {
    2.     myPlayerSpeedFloat = normalizedSpeed * myPlayerMaxSpeedFloat;
    3. }
     
  6. AwesomeX

    AwesomeX

    Joined:
    Sep 10, 2013
    Posts:
    117
    Well, basically I'm trying to achieve what you said in your first suggestion, by using a simple variable like this.

    Code (CSharp):
    1. public float speed = 125.0f;
    The problem is, when I check for my speed float variable on the OnValueChanged() function, it just doesn't show.

    As for your second suggestion, that's what I'm using currently, but I'm wondering if the first suggestion is possible?
     
  7. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Nope, Sliders dont bind to fields. They can only call methods.
     
    deram_scholzara likes this.
  8. deram_scholzara

    deram_scholzara

    Joined:
    Aug 26, 2005
    Posts:
    1,043
    Oh right! I've never actually tried it with a field now that I think about it =-P
     
  9. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Yes, and as _Daniel_ says, properties will work as well due to them secretly being methods under the hood.
     
    Melang likes this.
  10. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    Just in case anyone here didn't know the difference between a property and a field (I didn't :oops:) :

    Code (csharp):
    1. public float speed {get; set;}
    this is a property and it CAN be bound to a slider
     
    deram_scholzara and User340 like this.
  11. EvalDaemon

    EvalDaemon

    Joined:
    Aug 8, 2013
    Posts:
    107
    Oh wow! So attaching the below code to an new UI Slider....

    Code (CSharp):
    1.     public float gamma;
    2.     public float UISetBrightness
    3.     {
    4.         get
    5.         {
    6.             return gamma;
    7.         }
    8.         set
    9.         {
    10.             gamma = value;
    11.             RenderSettings.ambientLight = new Color(gamma,gamma,gamma,gamma);
    12.         }
    13.     }
    AWESOMENESS!