Unity Prefabs

Posted on

in

,

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 *

About me

Mark Wong is a front end developer with 10+ years experience. Most of his knowledge of HTML5, CSS and Js is self taught.

Calendar

December 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031