i recently try out unity and saw in one of the youtube video clip they code like this:
Random.Range(0, 2) == 0 ? -1 : 1;
i understand the Random.Range() but what does the the part after mean? thank you in advance
i recently try out unity and saw in one of the youtube video clip they code like this:
Random.Range(0, 2) == 0 ? -1 : 1;
i understand the Random.Range() but what does the the part after mean? thank you in advance
Its the Ternary Operator in C#.
Example:
condition ? expressionResultForTrue : expressionResultForFalse
If Random.Range(0, 2) == 0
evaluates to True
we get -1
else 1
Random.Range(0, 2) == 0 ? -1 : 1;
This means, return -1
if Random.Range(0, 2) == 0
, else return 1
Read on Ternary operator in c#
Accordinaly to Unity documentation, Random.Range receives two parameters, minimum (inclusive) and maximum (exclusive). So it will return 0 or 1. If Random returns 0, this line will return -1, else 1.