How to fix MissingReferenceException?

I have been following this tutorial on how to delete an object when a condition is met: Unity Endless Runner Tutorial #5 Destroying Platforms and adding some random space - YouTube. In the tutorial, only after the player passes a platform, will that platform will be deleted. However, in my situation, the moment the game is played, the referenced platform is deleted even before my player can pass it. And because of this, no other platforms can be generated.


public class PlatformDestroyer : MonoBehaviour

{
public GameObject platformDestructionPoint;

void Start()
{
	platformDestructionPoint = GameObject.Find ("PlatformDestructionPoint");
}
void Update()
{
	if(transform.position.x < platformDestructionPoint.transform.position.x);
	{

		Destroy (gameObject);

	}
}

}


The tutorial is in 2D and my game is in 3D, so I don’t know if that can affect anything. The error says, “MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.” Sorry I’m not too good at explaining things. Please ask if you need more information. Thank you for your time!

So first of all let me explain the error message you get:
When your if statement becomes true, it will stay true for as long as the player is on the right side of the platform. Therefore each frame that this is the case, the game tries to destroy the platform, but it can only be destroyed the first time. That’s why you receive that error.

To your actual question:
2D and 3D makes a big difference, in 2D it is enough for you to check for one dimension as was done in the tutorial (position.x). In 3D this is not sufficient.

My advice for you is to abandon that tutorial and look into triggers instead. Add a collider to your platform object and attach a script to it which has a OnTriggerExit method. In there you will destroy the platform if the other object of the collision is the player.

If you want to read more about colliders have a look at the manual:

You can find the method you need here: