My random number function keeps returning not allowed numbers, why? What's the solution?
I want the function to return button indexes that are interactable, I'm able to make it work when I put GetRandomValue function content in Update() function but I really want to keep them separate. Please help, here's the code fragment:
//
//
void Update() {
if (PlayerMove ==false) {
delay += delay * Time.deltaTime;
value = GetRandomValue(0, buttonList.Length);
if (delay >= 100) {
buttonList[value].text = GetComputerSide();
buttonList[value].GetComponentInParent<Button>().interactable = false;
EndTurn(ComputerSide);
}
}
}
public int GetRandomValue(int lowerLimit, int upperLimit) {
do {
value = Random.Range(lowerLimit, upperLimit);
return value;
}
while (buttonList[value].GetComponentInParent<Button>().interactable == true);
}
What do you mean by not allow numbers? Are the numbers out of range for your list, or do mean your getting NaN back?
Thanks fot the reply:) I wanted the computer to randomly choose and 'click' buttons that it didn't choose before. Everytime it has to be a new button. I already managed to do that by 'goto': //
//
public int GetRandomPossibleButtonIndex(int lowerLimit, int upperLimit) {
rng:
value = Random.Range(lowerLimit, upperLimit);
if (buttonList[value].text == "" && moveCount<=8) {
return value;
}
goto rng;
}
Your answer
Follow this Question
Related Questions
How can you do borderlands style random weapons and stat generation? 2 Answers
Choose something randomly based on its grouping 0 Answers
What is the difference between System.Random and UnityEngine.Random? 3 Answers
How to pick two random gameobjects in an array at the same time? 1 Answer
null array? 0 Answers