Creating Prefabs for 2D “Shmup” Bullets
What is a Prefab? Why and how do I use it?
Objective: Explain the basics of Prefabs and how to create a “Bullet” that can be used as a Prefab.
Prefabs are an incredibly useful tool any time you have more than one of the same object. For example, in the 2D “Shoot ’em up” that I’ve been making across these articles, there are going to be a ton of bullets. Since all of the bullets will function the same, there’s no need to repeat the steps every time you make one. This is what Prefabs are for.
To start, I’m going to create a 2D sprite to be a placeholder bullet. You can find out to do that here.
I’ve also changed the size, position, scale, and color to better represent a bullet for now.
Now, we need a folder to keep our Prefabs in. Keeping a tidy and organized work space is very important.
Making a Prefab is actually very easy. You simply need to drag an object from your Hierarchy into your Project folders. This will cause the Object to be highlighted blue to show it is different from other objects.
Prefabs can be used in a lot of interesting ways, such as spawning in the bullets and keeping all new bullets exactly the same as the original. But another cool thing that you can do, is if you make a change to one bullet, you can save the change to all other bullets, including the original.
Here, I’m going to create a second bullet, and add a “Capsule Collider 2D” Component.
You can see by the highlighted blue section on the side, that the Capsule Collider 2D is unique to that bullet and hasn’t been saved to the others yet.
In order to save this change across all bullets, you want to use the “Overrides” drop-down in the top right. Here you can “Revert All” which takes away any changes, or “Apply All” which save the changes to the original Prefab and any duplicates.
That’s the basics of Prefabs! In the next article we’ll sort out how to spawn prefabs.
Friendly Reminder: Don’t forget to keep updating your project through GitHub/Git and saving any changes you make to a separate branch. You can check out my other articles to get a breakdown of those steps! Day 1–9