-1

This question got marked as duplicate as it involves creating dynamically named variables. But the thing that I'm after is to create a number of dynamic sliders, so those .net answers aren't helping me.

The sliders are the same physical size and their names are held within an array, and their number is known. However, it could be one it could be twent, hence the need for dynamic slider creation.

This doesn't work:

for (int i = 0; i < numOfCheeseMonkeys; i++)
{
    slider + i = GUI.HorizontalSlider(new Rect(25, 50 + (i * 30), 150, 25), slider + i, 0.0F, 100.0F);
}

...this will, but it's stoopid:

sliderA = GUI.HorizontalSlider(new Rect(25, 50 + (i * 30), 150, 25), sliderA, 0.0F, 100.0F);
sliderB = GUI.HorizontalSlider(new Rect(25, 50 + (i * 30), 150, 25), sliderB, 0.0F, 100.0F);
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
  • The work around is to use a [collection of some sort](http://msdn.microsoft.com/en-us/library/ybcx56wz.aspx) – p.s.w.g Apr 01 '14 at 16:20
  • # p.s.w.g You mean, like a list array? – Ghoul Fool Apr 01 '14 at 16:22
  • Yes, a list or an array might be a good option. You might also consider a dictionary, but without knowing exactly what you intend to do, I can't say which collection you should use. I'd suggest you start by reading that MSDN article and familiarizing yourself with the various types .NET provides and thinking about how you can apply them in your code. – p.s.w.g Apr 01 '14 at 16:23

1 Answers1

1

Use an array or some kind of collection. It would look something like this:

Slider[] sliders = new Slider[numOfCheeseMonkeys];
for (int i =0; i < numOfCheeseMonkeys; i++)
{
    sliders[i] = GUI.HorizontalSlider(new Rect(25, 50+(i*30), 150, 25), slider[i], 0.0F,   100.0F); // yeah, this doesn't work

}
SimpleJ
  • 13,812
  • 13
  • 53
  • 93