What are prefabs in Unity?

In Unity, prefabs are a powerful and essential feature for creating and managing reusable objects and GameObjectsThe fundamental objects in Unity that represent characters, props and scenery.. It is a template or a blueprint that allows you to define a GameObject with its components, properties, and child objects and then instantiateA function of the Object Class that allows you to spawn new objects in your scene, from code, as your game runs. multiple instances of that GameObject throughout your scene.

You create your customized prefab, and once it is created, you can easily instantiate a copy of it in your scene with all the properties you have defined. Any change that you will make to the original prefab will be reflected in all of its instances.

Create a cube prefab

canvasAnimation-image
1 of 10
  1. Create a simple 3D project in Unity Hub.

  2. Click on GameObject in the top panel. Select "3D Object", then "Cube."

  3. In the top-left, you can see different tools.

    1. View tool: This tool allows you to view your object from different directions. Click and drag the object using the view tool.

    2. Move tool: With this tool, you can move your cube. Hold the arrow in which direction you want to move and drag to move in that direction.

    3. Rotate tool: Using the rotate tool, you can rotate your object from different sides. The circular outlines act as a guide to help you turn your object.

    4. Scale tool: This tool allows you to scale your object. If you select it from the center and drag it, the cube will scale up or down in all directions. If you choose other scales, it will only scale in that particular side.

    5. Rect tool: With the rect tool, you can change the 2D dimensions of your object.

    6. Transform tool: Using the Transform tool, you can precisely position, rotate, and scale game objects, helping you to arrange and design your scenes efficiently.

  4. You can customize the cube with a downloaded image or a material that you've created. Drag the image or material onto the cube.

  5. Once you are done with creating your object, drag it into your prefabs folder.

Now you can use your prefabs in your Unity projects. By building prefabs as reusable templates for your game objects, you can streamline your workflow and save time on redundant tasks.

Instantiate prefab

In Unity, Instantiate is a function used to create instancesCopies. of prefabs or GameObjects during runtime. It allows you to spawn new instances of objects in the game world dynamically.

The Instantiate function takes two main parameters:

  1. The Prefab or GameObject: You provide a reference to the prefab or GameObject that you want to create copies of.

  2. Position and rotation: You specify the position and rotation where the new instance will be placed in the scene.

Here's the basic syntax of the Instantiate function:

public GameObject cubePrefab;
void Start()
{
Instantiate(cubePrefab, new Vector3(0, 0, 0), Quaternion.identity);
}
Instantiate prefab code

Here, we've used the Instantiate method in the Start method. This will instantiate the cube when the game starts.

Nesting and composition

You can create nested prefabs by dragging and dropping one prefab into another. This allows you to build complex game objects with multiple combined prefabs. You can make changes to the nested prefabs independently, which will be reflected in all instances of the parent prefab.

Deleting a prefab

To delete an instance of a prefab during runtime, you can use the Destroy() function.

//creating a prefab
GameObject prefabInstance = Instantiate(cubePrefab, new Vector3(0, 0, 0), Quaternion.identity);
//deleting a prefab
Destroy(prefabInstance);

To delete the cube prefab from your assets, locate the file, right-click it, and select "Delete." This will remove the prefab from your project.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved