Search Unity

Change Slider value with click

Discussion in 'Scripting' started by chemsoun, May 23, 2015.

  1. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    hi

    i'm having a little trouble in my unity game and i'm hoping to have a little help here.
    i have some clickables GO in my scene, i need to show a panel with a Slider as children whenever a GO is clicked, the slider when showed should increase it's value progressivly until it's filled.
    My problem is that the Slider increases it's value one time (not until its filled) as i clicked over the GO one time, what i want is that the Slider should increase it's value until it's filled everytime i clicks over a GO

    i'm using this code but it's not working

    Code (CSharp):
    1. void OnMouseDown(){
    2.         GameObject.Find ("CanvasAliments").GetComponent<CanvasGroup> ().alpha = 1;
    3.         Slider niveau = GameObject.Find ("CanvasAliments").transform.FindChild ("Panel").transform.FindChild ("Slider").GetComponent<Slider> ();
    4.         niveau.value += Time.deltaTime;
    5.             if (niveau.value == 1) {
    6.                 Debug.Log ("done");
    7.             }
    8. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Put a breakpoint on line 4 and see what the niveau.value is after each click.

    Keep in mind Time.deltaTime is usually quite small, 1/50th of a second or so, so it will take a lot of clicks to get the value up to 1.