How to put null & non-null value calculation in 1 if statement

Hi

Here is my current code:

        if(_path != null)
		{
			if(_currentWaypoint >= _path.vectorPath.Count)
			{
				endMovement();
			}
		}

Ideally I wanted the code to read:

  if(_path != null && _currentWaypoint >= _path.vectorPath.Count)
  {
        endMovement();
  }

However because _path is sometimes null, get a null reference error when I try the above code.

Is there a way I achieve having both logic checks in 1 if statement?

You may get a null reference exception if the _path.vectorPath is null.

if (_path != null && _path.vectorPath != null && _currentWaypoint >= _path.vectorPath.Count)
{
    endMovement();
}

Not that I know of. You could avoid checking it, if you take care that it always exists (like I hope you do with the VectorPath list inside the _Path!)

But here is a syntax variant that you might like. It uses the ? operator:

if( (_path!=null)? _currentWaypoint >= _path.vectorPath.Count : false )
{
   Stuff();
}