Search Unity

Hooking Up To A Slider

Discussion in 'Getting Started' started by Jeff-Goin, Jan 31, 2015.

  1. Jeff-Goin

    Jeff-Goin

    Joined:
    Jan 29, 2015
    Posts:
    11
    I'm trying to make slider "Throttle" set a value. There is no gameObject value, rather I'm trying to change a value in a script. I've attached my ThrottleControl script to the "On Value Changed" component in the inspector. The script includes a public function with a float parameter.

    I've watched the "Slider" tutorial and it says that this should expose that function so that I can select it in the inspector. It doesn't. Any ideas what newbie mistake I'm making here?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class ThrottleControl : MonoBehaviour
    7. {
    8.    public Slider sliderVar;
    9.    public float ThrottlePosition;
    10.  
    11.  
    12.    public void SliderTest (ref float passedValue)
    13.    {
    14.      Debug.Log ("Throttle onValueChanged, Slider = " + passedValue);
    15.      //return(0f);
    16.    }
     
    Last edited by a moderator: Feb 2, 2015
  2. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    60
    Is the ref property necessary? Try without it. When you click the dropdown to select the function to call, make sure you select the callback under the "Dynamic Float" option so that it will pass the value that the slider currently is.

    Also, if you are just interested in updating a value, you can do it as a public property of your script instead:
    Code (CSharp):
    1. public float mySliderFloat { get; set; }
    And you could even bind to the set itself to do something if it gets changed..
    Code (CSharp):
    1. private float _myFloat
    2. public float MyFloat
    3. {
    4.     get { return _myFloat; }
    5.     set
    6.     {
    7.         _myFloat = value;
    8.         // do something with value here
    9.     }
    10. }
     
  3. Jeff-Goin

    Jeff-Goin

    Joined:
    Jan 29, 2015
    Posts:
    11
    Thanks for looking at this.

    The ref was a shot in the dark. I've included a screenshot after writing a very simple script that makes two public functions available that I would have thought to meed the criteria. As you can see, I've dragged "TestMyFloat" into the "On Value Changed" property box, clicked on the function, but nothing shows up.

    Have my newbie eyes missed something painfully obvious?

    Jeff G.

    Code (CSharp):
    1.  
    2.  
    3.     public class TestMyFloat : MonoBehaviour
    4.     {
    5.        public float MySliderFloat2(float passedThing)
    6.        {
    7.          return 0f;
    8.          Debug.Log ("MySliderFloat2 ran");
    9.        
    10.        }
    11.        public float MySliderFloat()
    12.        {
    13.          return 0f;
    14.          Debug.Log ("MySliderFloat ran");
    15.        }
    16.     }
    17.  
    18. }
     
  4. Deleted User

    Deleted User

    Guest

    the method signature you use is wrong. You cant return a value.
    Code (csharp):
    1.  
    2. public void OnValueChanged( float value )
    3. {
    4.     Debug.Log( value );
    5. }
    6.  
     
    LunaticEdit likes this.
  5. Jeff-Goin

    Jeff-Goin

    Joined:
    Jan 29, 2015
    Posts:
    11
    OK, I tried that, still no luck. Here is a script that I have attached to my slider (which I don't think is required). Sill I don't have any valid choices in the function dropdown.

    Any help will be appreciated!

    My test code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SliderFloatTest : MonoBehaviour {
    5.        
    6.  
    7.     public void OnValueChanged(float value)
    8.     {
    9.         Debug.Log ("OnValueChanged, value=" + value);
    10.     }
    11.    
    12.     public float OnValueChanged2()
    13.     {
    14.         Debug.Log ("float OnValueChanged2 ran");
    15.         return 1.1f;
    16.     }
    17.    
    18.  
    19.     // Use this for initialization
    20.     void Start ()
    21.     {
    22.         Debug.Log ("TestMyFloat started");
    23.     }
    24.    
    25. }
     
  6. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Code (csharp):
    1.  public class ThrottlePositionScript : MonoBehaviour
    2. {
    3.  
    4. public float ThrottlePosition ;
    5.  
    6. .                public void AdjustThrottle (float NewestThrottle)
    7. .             {
    8. .             ThrottlePosition = NewestThrottle;
    9. .              }
    10. }
    Then in the editor, on the relevant slider click the + symbol, drag your 'ThrottlePositionScript in, and select the AdjustThrottle function... It's possible I have missed your point, but this is how I have my slider set up and it works 'spiffingly' :D
     
  7. Jeff-Goin

    Jeff-Goin

    Joined:
    Jan 29, 2015
    Posts:
    11
    Thanks for taking the time.

    I've uploaded a screenshot with arrows to show exactly what I did and what I saw when I tried to see the function. It's still not working -- I don't see the "AdjustThrottle" in the drop down list. Puzzling indeed -- I've gotten everything else working, and even a slider using a completely different method, but not what should be the easiest method. No doubt I'm making some weird little error somewhere.

    If you look at the uploaded screenshot, here's what the numbers correspond to:
    1. File | Creat new project.
    2. GameObject | UI | Slider -- drops a slider in the scene.
    3. Project | Create new script, call it ThrottlePositionScript.
    4. Write the script, copied from your post. F8 to make sure it compiles, save.
    5. Select the Slider then add an "On Value Changed" space (at 7)
    6. Drag the slider script into the "On Value Changed" space.
    7. Click on the function dropdown.

    As you can see, no functions are available.

    Appreciate the help,

    Jeff G.

     

    Attached Files:

  8. Deleted User

    Deleted User

    Guest

    ahh no. you need to apply the ThrottlePositionScript to a gameobject. Any will do. Than drag the gameobject where the script is attached to, to the sliders OnValueChaged callback
     

    Attached Files:

    Jeff-Goin and Genkidevelopment like this.
  9. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    What I have learned to do... (As element_wsc suggests)... If you have random scripts such as these, attach them to an empty gameObject, you could call the object 'ThrottleScriptObject' or whatever... (You might want to parent these random scripts onto another empty gameObject, say called... 'ScriptPlacement' (To keep the heirachy clean and tidy) Then You drag this new gameObject onto the '+' part of the slider... This will allow you to find the script reference you have created... It seems its all about 'objects'... Scripts need to be on an object... :D Good luck, let us know if you have cracked it, or if you need further help :D
     
  10. Jeff-Goin

    Jeff-Goin

    Joined:
    Jan 29, 2015
    Posts:
    11
    Dang you rock. element_wsc I only wish I could buy you beer/wine/helicopter ride, etc.

    That's all it took.

    Genkidevelopment, I'll take your advice from now on and attach scripts to objects. I had kind of arrived at that conclusion when creating my own classes but figured it was a kluge. Maybe not. And hey, it works.

    Thank you!!!

    Hopefully someday I'll be able to return the favor to another newbie.