Instantiating & Destroying Gameobjects in Unity

Day 7- Making 2D Space Shooter Game

Instantiate is a method that brings a GameObject into existence. So basically it is a method used for cloning of objects in your project.

We call Instantiate() method in our script. It is available in MonoBehaviour, takes in a GameObject as a parameter, so it knows which GameObject to create or duplicate. We use prefabs as our GameObject to Instantiate.

Prefabs are a special type of component that allows fully configured GameObjects to be saved in the Project for reuse. These assets can then be shared between scenes, or even other projects without having to be configured again. In simpler terms, It is a gameobject that contains all the components and functionalities when created in a game.

Here, _laserPrefab is a variable of type GameObject which contains the prefab to Instantiate, transform.position sets the position to the created gameobject. Here, I am using Instantiate inside another gameobject so I am using the position of parent gameobject. Quaternion.identity is used to set the prefab to no rotation. Otherwise the instantiated object will have same rotation as of prefab.

If you want to define your own position then you can do it by adding a new Vector position but if you do as above then the instantiated gameobject will always spawn at the same position of (0,0.8f,0).

Since I am creating a Space shooter game and I am trying to Instantiate a laser from Player script when the player press space key, I am taking the current position of player and spawning the laser 0.8f value above in y-axis from the origin of the player without rotation.

Here, This script is written in the Laser script which is a component of laser prefab. As you can see, I am using transform.Translate to move my laser upwards like when the player is shooting.

The if condition checks the position of the laser in y-axis and if it is greater than 8.0(value when laser goes outside of screen) then it destroys the gameobject.

Destroy() method deletes the gameobject from the scene view and the parameter it takes is the gameobject it is going to remove.

In above image, I am removing the current gameobject when it reaches 8.0 in y-axis but you can explicitly tell which gameobject to remove and it isn’t bound to current gameobject only.




Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Search in Rotated Sorted Array

Galen Wolfe Pauly and Urbit

10 Quality Checks You Should Do For Your Website

Controlling the Project Financials

Using Scrum for “Agile” Software Development

RF Instrument Calibration

Intermediate vs Terminal Operations

How to build multiple web apps with Elixir thanks to umbrella — part 2: Set up the project

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
Niraj Karki

Niraj Karki

Unity Developer

More from Medium

How to the Layered Lit shader to mix Texture Maps in Unity HDRP

Cinemachine and Group Targets in Unity

OnClick Event in Unity

All about Sky in Unity’s HDRP