-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
  • 10,013
  • 18
  • 97
  • 186
TheLeader
  • 19
  • 1
  • 5

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,572
  • 7
  • 65
  • 91
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,240
  • 1
  • 16
  • 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
  • 1,184
  • 1
  • 11
  • 19