How can I set the order of destruction of game objects?

I am trying to guide a player along a path with stationary game objects leading his way (like following a trail of breadcrumbs). I don’t want the player to be able to destroy, for example, the 3rd guiding point object if he hasn’t already destroyed the 2nd. I wrote this code which I was sure would work, but instead my player just collides with the object, even if the one assigned in the variable has been destroyed. The code is attached to each object along the path, with the variable ‘previous’ on each object assigned to the object before it. I’m pretty new to coding so if anyone has any suggestions or code that I could look at it would be much appreciated. Thanks!

public GameObject previous;
void OnCollisionEnter () {
      if(previous = null) {
        	Destroy (gameObject);
        	}
        					
}

Try this:

public GameObject previous;
void OnCollisionEnter () 
{
      if(previous == null) 
      {
            Destroy (gameObject);
      }
}

More information from this article. Quoted because the rest of the article could be confusing. Note they talk about javascript but the idea is the same. Using a single equal sign (=) means something different from using two equal signs (==).

(Everything below is not mine - Quoted from this article)


Assignment versus equality

A mistake novel JavaScripters frequently make is confusing the assignment and equality operators = and ==. If you want to compare two values, you should use a double equal sign since that’s how JavaScript works. If you accidentally use a single equal sign, you don’t compare values but assign a value to a variable.

For example:

if (x == 3) {
	doSomething();
}

This is correct: you compare the value of x to 3. If x in fact has the value 3, the doSomething() function is executed.

This is wrong:

if (x = 3) {
	doSomething();
}

Now you do not compare x and 3, but instead you assign the value 3 to x. The doSomething() function is always executed, regardless of the original value of x. This can be quite confusing to novel scripters who think they’ve done it right.

Now why is doSomething() executed? That happens because the assignment operator = also returns a value: the value you just set the variable to. In our example, the x = 3 statement does two things:

It assigns the value 3 to x.
It then returns the same value 3 to whichever JavaScript construct asked for it. In this case, it’s returned to the if () statement.
The if () statement expects to receive a boolean value true or false. If it receives true the statement doSomething() is executed. If it receives false the statement is not executed.

However, now if () receives the value 3, which is a number and not a boolean. That’s perfectly OK to JavaScript. If any operator or statement receives a value of the wrong type, JavaScript silently converts the value to the correct type. Chapter 5 of the book treats these situations in detail.

The number 3 is converted to the boolean true and doSomething() is executed. In fact, almost every number is converted to true, and therefore doSomething() will be executed no matter which value you assign to x.

The only exceptions are 0 and the special value NaN (Not a Number). These two are converted to false.

Usually you don’t want all this complicated stuff to happen. Usually you just want to compare two values and take action based on whether they’re equal or not. That’s why you generally use the equality operator == in such cases.