Alan Fletcher
Alan Fletcher

Reputation: 283

Unity3d with c# script

Hello I would like to add a slider to my unity3d project, I am using c# script and the following code.

 public float hSbarValue;
 void OnGUI() {
         hSbarValue = GUI.HorizontalScrollbar(new Rect(25, 25, 100, 30), hSbarValue, 1.0F, 0.0F, 100.0F);
    }

This draws a slider that i have to use the mouse to drag it.

I want to turn it into a slider that moves on its own (like ping pong effect) until a button is pressed and then store the value in the hSbarValue variable.

Any help is appreciated

Upvotes: 1

Views: 691

Answers (2)

Alan Fletcher
Alan Fletcher

Reputation: 283

hSbarValue = GUI.HorizontalScrollbar(new Rect(25, 25, 100, 30), Mathf.PingPong(Time.time*5.0f, 20), 1.0F, 0.0F, 50.0F);

Upvotes: 1

Brandon Buck
Brandon Buck

Reputation: 7181

You might try using a Coroutine to increase the value of it and kill the coroutine when the button is pressed. Something like:

IEnumerator IncrementValue() {
    while (true) { // Or a better limit
        hSbarValue += someIncrementValue;
        yield return new WaitForSeconds(1); // Or other value
    }
}

void Start {
    StartCoroutine("IncrementValue");
}

// later inside the button press handler
StopCoroutine("IncrementValue");

Upvotes: 1

Related Questions