The point of switch/case is that you hardcode every item. If you don’t want that, don’t use switch/case. In your code, just do “number = Random.Range(1, 4);”.
I believe it can be done without a giant switch. If you can give more information about what one of your case functions might look like, I could probably help further.
I display random pics. The number is the style number. So 1 is style 1, 2 is style 2. That way I change pics. It is randomly done to have kind of a slotmachine effect. 100 pics should be shown per action, but in random selection. That’s all.
I was just interested int technical side, because I feel really uncomfortable with case switch not being possible to write code in one line, not possible to enbed a while or foreach…
what your original code does is the same thing that eric posted… except his cuts the need for the whole switch stuff… why cant that work? the code is literally doing the exact same thing…
Because number will get a random value out of 1-4. That way I get one pic out of 4. I need different 100 pics displayed one after the other but randomly. Just the order should be another each time. You know big bang theory intro? his one but randomly everytime.
You need to give more info to get better help. Your sample code for example imply that you could have just done
int number = Random.Range( 1, 4 );
and skip the whole random_number and switch step.
So, are you trying to figure out filenames for your textures to load? Then just name them like “pic1.png”, pic2.png",etc and do
int number = Random.Range( 1, 4 );
string selected_file = "pic" + number.ToString() + ".png";
If textures prefabs then …
public Texture2D[] textures; // you added these in inspector
...
int number = Random.Range( 1, 4 );
Texture2D selectedTexture = textures[number];
Like Jaimi said, give us an example of what one of your “case” blocks looks like. I don’t think anyone can give you much usefull info if you don’t show us what you have attemped. I assume it is not just literally
Create a list of integers from 0 to N-1, where N is the number of pics.
Let list size be L.
Choose a random number from 0 to L-1, call it x.
Remove the x’th element from your list, this is the next image number (or just store image refs, either way).
Repeat with new list size L -1.
If it were me, I’d store my images in an array, the each time you want to effectively ‘shuffle’ your list of images, I’d create an list of indexes to match the array and keep removing one by one to determine next image index to return, or just use a shuffle algorithm in the first place, google Fisher-Yates.
Sorry I really did not know that my question will make such a wave. My question was not how to do it another way, but ok. Here is a short version of my code. Pressing a button 100 picture icons are displayed in random order. It’s so simple, I really don’t know how to tell it more easier.
My only intention for this thread was to shorten the hardcoding for 10, 100, maybe 1.000.000.000.000 icons with one simple loop or something like that.
Again, I don’t need one out of 1.000.000.000.000 icons. I need 1.000.000.000.000 out of 1.000.000.000.000 icons, but in a random order displayed. Like the youtube example - just with a new order of pics every time the button is pressed.
GUI.Label(new Rect((Screen.width - 220) / 2, Screen.height - 540, 50, 50), new GUIContent(""), change_icon + picnumber);
public void icon_randomizer() {
int random_value = Random.Range( 1, 11 );
switch( random_value ) {
case 1:
picnumber = 1;
break;
case 2:
picnumber = 2;
break;
case 3:
picnumber = 3;
break;
case 4:
picnumber = 4;
break;
case 5:
picnumber = 5;
break;
case 6:
picnumber = 6;
break;
case 7:
picnumber = 7;
break;
case 8:
picnumber = 8;
break;
case 9:
picnumber = 9;
break;
case 10:
picnumber = 10;
break;
}
}
Kinda a juxtaposition, but ok. People are giving you a different way because even your shorter version is still longer and unmaintainable. You are writing a case for every pic number and tbh I don’t see why you can’t write “picnumber = random_value” instead…
Eric gave you that answer already and as long as we aren’t getting more info/specs of what you want to accomplish, this will remain inadequately solved. For example, you posted the BB intro, but do you want to present these images in succession (second interval) or do you already have the code for that, etc… not enough info here.
If you use the cases for the pic selection as well (you should state that in your opening question), either have a naming scheme for the images so that this name can be generated in code(f.e. Img1, Img2, Img3, etc…), or have them all in the same folder and load them all to fill in an array, etc… So many ways to do this that isn’t hardcoding (avoid this at all times as it’s usually wrong).
Thats not what he’s after, he wants to have a list of images shuffled not a random pic from the list. So if he has 10 images, he wants those ten I ages to come back in a random order, ensuring each image is displayed only once per call and all images get one render, but in a random order each time.
So essentially, he wants to ‘shuffle’ his list of images, easily done if he takes the time to implement what I’ve posted.
I suspect they are trolling for some to post a working code solution at this point
why not use an array of the images, and randomize the array.
List<int> numbers = new List<int>();
List<int> shuffled = new List<int>();
for (int i = 0; i < 9; i++)
{
numbers.Add(i);
}
while (shuffled.Count != numbers.Count)
{
int rand = Random.Range(0, numbers.Count);
if (!shuffled.Contains(rand))
{
shuffled.Add(rand);
}
}