- Home /
SOLVED! - Pick Number != These Other Numbers
So I'm putting together a script that will pick a number that does not equal other numbers that are based on an instance. I've put together a scrip that does what I think it should do, however it returns a number it should not.
Code Summary: There is a range of numbers from 0 to 4. I'd like it to shuffle through these numbers and pick one that does not equal another set of numbers. However, it still picks the incorrect number.
do{var pick = Random.Range(0,5;}
while(pick != Number0 && pick != Number1 && pick != Number2 && pick != Number3 && pick != Number4);
The correct answer in this case, would be 0 or 1, but it picks 3.
Sorry, do should read like this:
do{var pick = Random.Range(0,5);}
Answer by VigorousApathy · Jan 16, 2013 at 05:22 PM
SOLVED IT!
Here is the code that does what I intended:
while(picked == false)
{
var pick = Random.Range(0,5);
if(pick != number0 && pick != number1 && pick != number2 && pick != number3 && pick != number4)
{
yield pick;
picked = true;
}
}
Answer by fafase · Jan 16, 2013 at 01:21 PM
I think your while condition should be the other way around. As long as what is inside is true the condition repeats. I guess you want the invert effect.
Your answer
Follow this Question
Related Questions
Can't instantiate in loop - crashes unity 1 Answer
Infinite loop when I try to generate randomly a 2D dungeon.. 3 Answers
Trying to Loop A Function 2 Answers
How to make a loop for PowerUps/Bonus 2 Answers
Loop a Function a Random Number of Times 2 Answers