< Back to Game Dev, Unity

Unity Prefabs

What are prefabs? They’re game objects that are reusable. Examples of these could be collectable coins or enemies or enemy projectiles that appear multiple times in the game. To make a prefab, simply make a game object as always, then drag it into the assets folder (ideally in a folder called Prefabs or the like). Any components like colliders and scripts will be retained.

To add this prefab with code use:

GameObject myPrefab = (Instantiate (Resources.Load ("Prefabs/prefabName")) as GameObject);

Change the path to your prefab if it isn’t in a folder called “Prefabs”.

You will also usually set this prefab as a child of another element. To do this use:

myPrefab.transform.parent = nameOfParentObject.transform;


Leave a Reply

Your email address will not be published. Required fields are marked *