Unity Prototyping: Instantiating and Destroying GameObjects

Whether you are just starting out as a game designer or have been making games for years, prototyping is an essential skill to master. It allows you to create games by quickly testing out new game concepts and mechanics without needing to create the final assets. Using simple objects like cube, sphere or capsule, you can make your own playable demos in hours or days, rather than weeks or months.

Instantiating GameObjects and Prefabs

Unity’s Prefab system allows you to create, configure, and store a GameObject with all its components, property values, and child GameObjects
as a reusable Asset. The Prefab Asset acts as a template from which you can create new Prefab instances in the Scene.

To create a Prefab just add any object to the hierarchy window and drag and drop it in the project window.

To instantiate a prefab, we will use a following function:

Let’s use this function to instantiate a laser prefab for a player:

When the space key is pressed, a prefabricated laser will appear at the player’s location.

Destroying Objects and Prefabs

To avoid overloading our game and memory with laser objects, it would be useful to destroy them after the laser disappears from view.

We can use the ‘Destroy(gameObject)’ function for this:

When the laser crosses position 11 on the y-axis, then our prefab will be destroyed.

Prototyping is one of the most important skills you can develop as a game designer. It allows you to rapidly test out new ideas, create playable demos, and refine your design before investing too much time or money in a final product.

Hi, I’m Damian, a Unity Software Engineer who loves building education and simulation games.