Using Prefabs in Unity

## Unity Prefab Guide

### Getting Started

This guide is going to require a bit of coding, but should be easy to follow along and understand. Prefabs are a "Prefabricated Item", which means they are packaged game objects within Unity, that you can repeat and alter as you wish. While this can be used to a variety of different things within Unity, I will be using enemy types and attributes as an example. If you wish to follow along exactly with the example, you can get the free starter sprites from this link.

*Note: The sprite we are using in this example is not created by me, but is free to use!*

#### Optional: Setting Up Example Sprite

To set up the sprite from the link, simply extract the zipped file into your Unity project. You will see a bunch of different sprites, which should look similar to this:

For this project, we will be modifying the damage output from this character. To show the damage visually, we will be using the spr\_StrikeSlash\_stripNoEffect image strip, and turning it into an animation. To do so, simply click the sprite from within Unity. From there, in Sprite Mode, select Multiple and then click the Sprite Editor.

      Once open, select Slice at the top of the editor. A small menu should pop up. Within that menu, ensure Type is set to "Automatic", and then click the Slice button at the bottom. If you now drag that sprite into Unity, you will be prompted to create a new animation. Once it is named, hitting "Play" at the top should show you what the animation looks like.

**Setting Up The Script**

Now that we have an enemy that is attacking, we need to set up a simple script for it to actually deal damage. Here's a sample script to get us started:

```
using UnityEngine;
using System.Collections;

public class BaseMeleeEnemy : MonoBehaviour {

 // Serialized Fields make variables from your scripts show up in the
 // inspector as if they were public variables
 [SerializeField]
 int damage;

 [SerializeField]
 int health;

 // Update is called once per frame
 void Update () {
 doDamage(damage);
 }

 void doDamage(int dmg){
 // implement your attack damage. This can vary from game to game,
 // so is left blank.
 }

 void takeDamage(int dmg){
 // simple way of other objects to cause damage to this character and
 // kill it once health reaches 0
 health -= dmg;

 if (health <= 0) Destroy(gameObject);
 }
}

```

Make sure this script is attached to the enemy, and you will see the serialized fields show up. The script can be attached by clicking the enemy, and then going to "Add Component" in the bottom of the inspector. If you type in the name of your script, it should pop up and you can hit enter to attach it. Once done, it should look like this:

### Creating The Prefab

Now that our character is set up, we can turn them into a prefab. For context, a prefab is essentially a packaged item, with set attributes and settings so that they can be easily set in a scene. For our enemy here, we can set the damage value to 10, and the health value to 15 as an example. After doing so, we can create folder to store all our prefabs, then click and drag the character from the inspector into the folder. This will automatically turn them into a prefab.

If we drag the prefab from our folder into the scene, we will see the exact same character with the exact same damage and health values. So now, we can spawn as many enemies as we need with these built-in values!

### Updating Prefabs

Let's say after some playtesting, you notice that the current damage value is way too low. If you already have a lot of these enemies set in your scene, changing all the values manually can be daunting. Here, the prefab saves us from doing unnecessary work. Simply navigate to where you saved it, and click on it. The inspector should pop up with all the components and attributes of our enemy, and we can change the numbers as we see fit.

### Further Implementations

Prefabs are not limited to just this use case, you can use them to create a base-template for characters within your games, and use the attributes to create variations. For example, we can copy the prefab of the enemy from this tutorial and change the values to turn them into a tank. We can then change the sprite (also a component we can modify through the prefab) to have them visually reflect another role, like a tank or a healer.

You can also create nested prefabs, meaning you can place a prefab within an already existing one. This can be useful for a variety of things, for example giving certain enemies various projectiles they can shoot.

Not all prefabs have to be used for enemies, or attack purposes. Getting creative with the flexibility of them will allow you to speed up development significantly, and allow for more frequent playtesting. This is crucial to adjust attributes and get the game right, whether it be based on personal playtesting or feedback from others.

Hashir Ali