Tip of the Day: Creating and Destroying in Unity


Creating a gameObject in Unity is called Instantiation and the syntax for it is:


You can further extend this command by specifying the location where you want to instantiate the object and its rotation upon instantiation.

Instantiate(gameObjectPrefab, transform.position, Quaternion.identity);

Quaternion.Identity is used when you do not want to mess with the rotation of the object upon instantiation.

This command is useful in many cases; for example in our space project, we used it to create lasers upon hitting the space button.

Instantiating laser upon pressing Space


Destroy GameObject is used when you want to destroy an enemy for example, destroy a pickup after using it, or it is very helpful to use it to declutter the hierarchy during game play.

The syntax is

Destroy(this.gameObject);//this can be extended to allow the gameObject to be destroyed after a certain delayDestroy(this.gameObject, 5f); 
//this will destroy the object after 5 seconds.
Destroy the enemy gameObject upon contact with laser and then destroy the laser too
Destroying the laser after it is fired if it does not hit anything to remove hierarchy clutter.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mohamed Hijazi

Mohamed Hijazi


A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/