c#, unity, unity tips and tricks, Unity Tutorials, unity3d

Difference Between Destroy and DestroyImmediate Unity Functions

Destroy_DestroyImmediate

Objective :

Objective of this Tutorial is to understand diffence between Destroy and DestroyImmediate unity function

Purpose of both function is same, to destoy gameobject, but there is very small difference, which is very crucial to understand, wrong use of these function can cause your app to crash

So first of all little bit of theory and then some code

Destroy() :

  •  This just destroy the game object passed as parameter.
  •  It set all the references to that game object to null at the end of frame.
  • Syntax: Destroy(myGameObject);

Let see it in code:

using UnityEngine;
using System.Collections;

public class DestroyTest:MonoBehaviour
{
 GameObject currObject;

 void Update ()
 {
   if (Input.GetMouseButtonDown (0)) 
   {
     currObject = new GameObject ("myObject");
   }
   if (Input.GetKeyDown (KeyCode.Space)) 
   {
     if (currObject) 
     {
       Destroy (currObject);
       //DestroyImmediate(currObject);
       string status = (currObject) ? "true" : "false";
       Debug.Log ("is GameObject there after Calling Destroy: " + status);
       Debug.Log (currObject.name); 
     }
   } 
 }

}

Create new script and add this code and add it to Main Camera and click play, and just left click to create new game object and press space bar to destroy the game object and check the log in console window you will notice that first log print the status “true“, and second log print the name of currObject which is “myObject” and other information about the object, so at this time currObject is not null

Destroy_log

 

Now let see DestroyImmediate

DestroyImmediate()

  •  As the name suggest it immediately destroy the gameobject passed as parameter.
  •  It set all the references to that game object to null immediately, it will not wait for the frame to end like Destroy() function.
  • Syntax: DestroyImmediate(myGameObject).

Now just uncomment the DestroyImmediate function in above code and comment the Destroy function

using UnityEngine;
using System.Collections;

public class DestroyTest:MonoBehaviour
{
 GameObject currObject;

 void Update ()
 {
   if (Input.GetMouseButtonDown (0)) 
   {
     currObject = new GameObject ("myObject");
   }
   if (Input.GetKeyDown (KeyCode.Space)) 
   {
     if (currObject) 
     {
       //Destroy (currObject);
       DestroyImmediate(currObject);
       string status = (currObject) ? "true" : "false";
       Debug.Log ("is GameObject there after Calling Destroy: " + status);
       Debug.Log (currObject.name); 
     }
   } 
 }

}

 

and click play and as earlier just left click to create new game object and press space bar to destroy the game object and check the log in console window you will notice that first log print the status “false“, and second log will print “null”

DestroyImmediate_Log

 

Conclusion:

  • Destroy() will set the object to null at the end of the frame and whereas DestroyImmediate() function immediately set object reference to null
  • Use DestroyImmediate with caution, according to the docs you’re supposed to avoid using DestroyImmediate.

 

If you have any thought, Please write in comments below

2 comments

  1. Pingback: Unity Interview Questions - Unity Geek

Leave a Reply

Your email address will not be published. Required fields are marked *