-1

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

Muhammad Faizan Khan
  • 9,161
  • 12
  • 85
  • 167
TheLeader
  • 19
  • 1
  • 4

3 Answers3

5

Its the Ternary Operator in C#.

Example:

condition ? expressionResultForTrue : expressionResultForFalse

If Random.Range(0, 2) == 0 evaluates to True we get -1 else 1

Sadique
  • 22,181
  • 7
  • 62
  • 90
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#

Arjun Mathew Dan
  • 5,125
  • 1
  • 15
  • 27
1

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.

Ricardo Silva
  • 964
  • 1
  • 9
  • 18