How Destroy chooses its target

void Start () {
	if(instance !=null){
		Destroy(gameObject);
		print ("dup destroyed");
	}else{
		instance=this;
		GameObject.DontDestroyOnLoad(gameObject);
	}
}

In the line with Destroy, If the program enters that part of the if statement then there are two gameObjects. How does the compiler know which game object to destroy?

Hi,

The if statement is checking for the original instance of the object which was created. If there is one, then the second instance which has been created, in this case is the object running the script, can destroy the GameObject this script is attached to.

Privacy & Terms