Scripting Question from Unity 2D project about "?" and ":" in C#

// If the prop is facing left, it should start on the right hand side, otherwise it should start on the left.
float posX = facingLeft ? rightSpawnPosX : leftSpawnPosX;

This is the code that is giving me issues. I apologize if this has been asked before; my attempts to google anything with “?” or “:” in it were not very fruitful.

Can someone explain this code to me? I think it’s something related to comparison statements, but I read it long ago and can no longer remember. Specifically, what are the functions of “?” and “:”?

Thank you for your time.

?: is the conditional operator.

condition ? first_expression : second_expression;

If the condition is true, the first expression is evaluated and returned, otherwise the second expression is evaluated and returned.

So in your case, facingLeft must be a boolean variable (true or false). If you were to use a method instead of the conditional operator, it would look something like this:

float GetSpawnPosX(bool facingLeft) {
  if(facingLeft == true) {
    return rightSpawnPosX;
  }
  else {
    return leftSpawnPosX;
  }
}