Prefab Instantiation & Destruction in Unity Game Engine

Modular Prefab

Let us consider a scenario. You are about to build a house from scratch. Now, would you go out and get/make a brick one by one or have them produced using a single design in bulk and then use them as need be. Of course, you would go with the second approach. Why? Because, it saves you time, effort and helps maintain design & behavior uniformity.

Similarly, in Unity, you can build modular pieces of game objects that you’d like to maintain a uniform look for. They are called prefabs.

I’d like to share a little example here that demonstrates the use of prefab and also destructing them when their need is over.

  • Think of a simple fighter plane that fires bullets. Would you go in Unity and build each bullet or create one and then use it as many times as you want? Yup, we will use the later solution by creating a prefab of a bullet.
  • Remember, when you create a prefab, please put it into a prefabs folder, and with each change, override the prefab so that it gets applied to all instances of prefabs to come.
  • First, create a 3d capsule and scale it so that it matches the size of your fighter plane.
Unity
  • Then, go ahead and apply the material of your choice and then right-click on the project window to create a new folder named “Prefabs”.
  • Now, drag the capsule into the Prefabs folder. This is going to convert it into a prefab.
Unity
  • Now, we want to fire bullets when we press the spacebar. Here comes the use of game object instantiation. It is needed so that you won’t have to bring in bullets manually but your code will do it for you. Plus, it has to happen relative to the position of your fighter plane.

Therefore, let us move to Visual Studio and write some code.

  • The first need is to attach this behavior to the plane object. Because it is through the plane, that the bullet would be instantiated. So, we’d declare a variable of type Game Object that is going to store this prefab.
C#
  • In Unity inspector, we will have to drag the capsule prefab into this variable to complete the attachment.
Unity
  • Now, we need to get the input from the spacebar and if that happens, we would direct Unity to instantiate the bullet at a certain position.
C#

Now, we are done with the first part. The next part is to let the bullet travel upwards.

  • Therefore, we create another script and attach it to the prefab of capsule named “Bullet”.
Unity
  • In the newly created script, please put in the following code to let the bullet travel in a positive Y direction. We multiply it with real-time and a speed value to have a smooth movement. (Please check out this article if you want to know how to add movement.)
C#
  • At this point, with each press on your spacebar, you should see a bullet going in an upwards direction. KUDOS!
Unity

The next part is the destruction of a game object.

  • For the sake of simplicity, I am going to write a code that would destroy the object, the moment it goes beyond the game view in the upward direction.
  • We will have to use destroy( ) method to accommodate this.
C#
  • If you observe in the hierarchy panel, you shall see bullet objects disappearing as soon as they go out of the game view.
Unity

‘This way, you can close out unwanted objects from your game and save memory.

Hope this was helpful :)

Thank you very much

--

--

--

Work in Progress.

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

Recommended from Medium

some more results

AWS VPC: Securely Subnetting with Private & Public Environments

How I increased my productivity using dotfiles

CVE Program Report for Q4 Calendar Year 2020

Raise your hand if you want to quit drinking coffee. Why, though???

Installing Ubuntu As A Development Environment

Enforcing AWS S3 Security Best Practices Using Terraform & Sentinel

ITiCSE 2021 Paper: CS1 Reviewer App

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
Samarth Dhroov

Samarth Dhroov

Work in Progress.

More from Medium

The simplest way to create a First Person Shooter! (Part 1)

Cinematography in Unity — Fade to Black

Adding a User Interface to our prototype

Installing Unity & Getting Started with Game Development