- Home /
Random Select (Scripting Problem)
So Once again noob question. SO what I'm trying to do is to generate a random number between 0 - 5 and use it in the "if" statement to call functions.
void Update ()
{
if (blockSelected == 0)
{
FunctionOne();
}
if (blockSelected == 1)
{
FunctionTwo();
}
.
.
.
if (blockSelected == 4)
{
FunctionFive();
}
}
Now I want to randomly select a function every time the Player hits the trigger. Can you guys please help how to do this?
you have everything there already, maybe what you're looking for is Random.Range?
int rand = Random.Range(0, 5+1);
Answer by Topthink · Apr 09, 2018 at 02:51 PM
Consider:
int tempInteger = Random.Range(0,6);
That will give you a number between 0 and 5 inclusive...the high number will be excluded. After this, you might include your "If" statements (Look at "switch" statements, might be more effective). See here: https://unity3d.com/learn/tutorials/topics/scripting/switch-statements
It sounds like you know how to use "if" statements. If you choose to use them, you would follow your Random above with your "if".
if (tempInteger==0) {...then do something here, call a function etc....}
if (tempInteger==1) {...then do something here, call a function etc....}
Hello Thank you very much. Follow up question. Is there a way to make sure the same number do not repeat until all the numbers in the range is used at least once?
Sure, there is a way to do almost anything. I did something similar in a project I'm working on now...I was selecting several objects from a list of several dozen and I didn't want to select the same item more than once. I used custom classes but it might be better to use a simple two dimensional array. There would be a lot of detail that goes with this explanation but here is a "High Level" explanation of one way to accomplish this. Use an Array that is size 6x2: put all your numbers (0-5) in one column and then put a zero (or whatever) in the second column. When you select a random number, change the zero in the second column to a "1" or something. Then, when you select the second "random" number, check the corresponding array column to see if it is already been selected (it would be a "1" or whatever). If it has been selected, then you would select another number (use something to loop it...many, many ways to loop). This would ensure that you select each number once before you select anything more than once. Again, there are so many ways to accomplish this...this would be just one of dozens, hundreds, etc. If you really need it, I could post up some of my code as an example, but at the point you are now, it might not mean much. Best wishes however.
Thank yo very much. Let me try and get back to you.
Having values in a random order is called "shuffling" (just like in playlists). Here is a good implementation: https://answers.unity.com/questions/16531/randomizing-arrays.html
These forums aren't intended to field questions about common program$$anonymous$$g encounters. Random number generation without duplicates is an exhausted topic on other forums and can easily be solved by Google: https://stackoverflow.com/questions/26931528/random-number-generator-with-no-duplicates
If this response answered your original question, then mark it as the solution so this question can be closed. If you have other basic C# questions in the future, please make sure no one else has already asked / solved a similar question.