What does this variable mean?

Hai, i saw this in the internet, and i couldn’t think what is this about:

they created the boolean which is named obstacles and set it to false.

public bool obstacles = false;

But, what is this line about:

obstacles = obstacles ? false : true;

        if (obstacles)
        {
            transform.renderer.material.color = new Color(.5f, .5f, 0.0f);
        }

        else
        {
            transform.renderer.material.color = Color.white;
        }

I know the if and else function, when there is an obstacles, turn the material to the assigned color, or if there is not an obstacles, turn the material into white color. But, what is this line about: obstacles = obstacles ? false : true;

Thanks… Sorry for this newbie question.

if (obj1.activeInHierarchy == false ) or if (!obj1.activeInHierarchy)

1 Answer

1

obstacles = obstacles ? false : true;

This is the ternary operator. A fancy way to say:

if(obstacle == true)
obstacle = false;
else
obstacle = true;

Might as well just use

obstacles = !obstacles

Ternary operator just works like this:

 variable = condition ? return this if condition is true : else return that

This is limited to 2 conditions separated with ? : and the little plus is that it returns the result and stores it in variable.

So in you case, it evaluates obstacles, returns false if obstacles is true and true if opposite then stores the value in obstacles.

Again, the guy probably just wants to show off that he knows and you don’t as it could be done:

obstacles = !obstacles;