OnGUI and a hundred sliders

Dear forum

function OnGUI () {
	slider_0 = GUI.HorizontalSlider ( Rect ( 20, 20, 100, 10 ), 5, 0, 10);
	slider_1 = GUI.HorizontalSlider ( Rect ( 20, 40, 100, 10 ), 5, 0, 10);
	slider_2 = GUI.HorizontalSlider ( Rect ( 20, 60, 100, 10 ), 5, 0, 10);
	// ... and so on ... //
		
	if( GUI.changed ){
		// yes something changed ... but which slider exactly? //
	}
}

Do I have to create lastValue-variables for each slider and compare every new slider value to the according lastValue to find out exactly which slider has moved … or is there a less crude way of locating the changed slider?

Note that I need the sliders on the same script because I want to arrange them with GUILayout.

For the sake of about 100 lines of code
~ce

var            howManSliders : int = 20; 
var            mySliders : float []; 
//var            myPrevValues : float []; 

function Start () { 
   	// stuff our array full of zeroes 
   	var tmp = new Array (); 
   	for (var i : int = 0; i < howManSliders; i++) { 
      	tmp.Add (0.0); 
   	} 
   	mySliders = tmp; 
   	//myPrevValues = tmp; 
   	// check array loaded properly 
   	Debug.Log(mySliders.length); 
} 

function OnGUI () { 
   	var j : int = 0; 
   	for (var ary : int in mySliders) { 
      	//myPrevValues[j] = mySliders[j]; 
      	var k : int = 15 * j; 
      	mySliders[j] = GUI.HorizontalSlider ( Rect ( 20, 20+k, 100, 10 ), mySliders[j], 0, 10); 
      	// check for change
		if (GUI.changed) { 
		    Debug.Log ("'"+j+"' changed"); 
		    GUI.changed = false;   // reset the value, so we can detect the next one 
		}
      	//if (mySliders[j] != myPrevValues[j]) { 
      	//	Debug.Log("slider '" + j + "' has changed!"); 
      	//} 
      	j++; 
   	} 
}

[edited] I stand corrected again! I’ve fixed the code above to use GUI.changed properly! When I tried it last night I overlooked resetting it to false after checking it! So simple.

Cheers.

ps. i love this new gui system! it’s kick arse powerful and fast! :wink:

That’s cool, I’ll go for the “compare old and new values” solution then.

Thanks thylaxene

You can check GUI.changed after each slider

myVar = GUILayout.Slider (myVar, 0, 1);
if (GUI.changed) {
    Debug.Log ("myVar changed");
    GUI.changed = false;   // reset the value, so we can detect the next one
}
   
myOtherVar = GUILayout.Slider (myOtherVar, 0, 1);
if (GUI.changed) {
    Debug.Log ("myOtherVar changed");
    GUI.changed = false;   // reset the value, so we can detect the next one
}

Or you can just use a for loop

Ha! Now we are getting somewhere. Great Nicholas!

=D