If/else statement is always else
I was coding more stuff and needed to make an if/else statement. But the statement always evaluates to else. Both variables are set randomly but the contents of redArray are the same every time the code is run. What is going on?
Code:
void PressButton1()
{
if (leftLEDColor == 1 && (redArray[rightLEDColor] == 1))
{
//do stuff
}
else
{
//do other stuff
}
}
If any more context is needed I will be happy to provide it.
One thing I could think of is that if you made a random number from 0-1 using Random.Range() the max value is exclusive and will never be chosen. Only other thing that could be (in my opinion) is that there is no 1 in the array... but something tells me that you have checked that.
Both random numbers are between 0-4 with the max value exclusive and there are multiple instances of the number 1 in the array, so it can't be those.
Answer by unfixedacorn · Jul 23, 2020 at 01:56 PM
So as it turns out, I'm dumb. I wanted to look for the smallest possible value and forgot that computers started counting from 0.
The code SHOULD be this:
void PressButton1()
{
if (leftLEDColor == 0 && (redArray[rightLEDColor] == 1))
{
//do stuff
}
else
{
//do other stuff
}
}
Whoops.
Your answer
Follow this Question
Related Questions
How to put an if statement inside an if statement? 3 Answers
How to use IF in Unity for selection MyScript 1 Answer
If Statement Help 0 Answers
Using "if" statements to designate movement limits 0 Answers
Problems with debug and if-statement 1 Answer