Exercise 2: Prefabs and parenting - VapaaLassi/TAMKUnity2024 GitHub Wiki
Prefabs make making games possible, they are one of the most important parts of Unity. If we think about what we have made this far, we only know how to copy existing objects. So if in the last scene, if we wanted to have 5 cubes, but later on would want to change all the cubes into balls, we’d be in trouble and very annoyed as we would one by one change them into something else.
So instead we are going to use prefabs. The word is a bit strange, but what it essentially means is a blueprint of an object. So instead of a cube, we place a blueprint of a cube. Then when the game starts, all the blueprint are replaced with whatever the blueprint tells them to be.
Another way to think about them could be an e-book or these instructions. Everyone downloads a copy of it onto their device. If there is a mistake in the text, it can be fixed and the updated version will automatically be updated on every device when it refreshes. This also means that we can go and modify a prefab and all the changes we make there are automatically updated to any copies of that prefab.
-
So let’s expand our old scene or start a new one. Let’s create a cube. Then let’s drag the cube from the Hierarchy tab into the Project part of the window, which is the easiest way to create a prefab. The name of the cube should turn blue in the Hierarchy tab.
-
Now we can add 4 more of these cubes either by pressing Ctrl+D or by dragging cubes from the Project window into the Hierarchy. Let’s move one around the scene and see what happens. After we move them, we notice that the position values in the Transform are now highlighted and bolded.
-
Then we can for example scale the cube larger or smaller and see that only the object we are changing changes. If we click on the "Overrides" dropdown next to the object, we can apply the changes and see all the copied cubes transform to the same size. We can press Ctrl+Z to undo that change.
-
A better way to do this, and a better way to understand where we are in the system is to open the prefab in edit mode. There is a very small arrow on the object you can click to get into Prefab editing mode.
-
Now in edit mode, any changes we make to the cube will automatically be applied. So let’s add physics to the cube (by adding a Rigidbody component), then go back to the scene and see what happens.
-
All the cubes should now fall down
Now, let's forget about the cube for a while and let’s talk a bit about parenting. If we want to build something bigger than one object, like if we want to build a robot that moves and has a more complicated collision system, we can’t really do that with one single object. So, to help with that, we can create collections of objects with hierarchy, which is basically placing objects "inside" (or under) other objects.
Objects can be children or parents of objects, or both. This is important both for organizing the scene, as well as functionality. When you move a parent, everything “inside” it also moves. It’s also significantly easier to communicate between objects that have parents or children. We will get to that when we get to scripting.
-
So let's actually build a robot! It should look something like this.
-
Start by creating an empty game object.
-
Then start adding cubes as children to that game object to build the robot. Make sure that the cubes don't have a rigidbody, but make sure they have a collider.
-
When your robot is finished. You can pick the Empty object from the Hierarchy, which you should probably name "Robot", and try moving it around. Now all of the cubes should move with the robot.
-
Then, add a rigidbody to the Robot object, and maybe rotate a bit forwards, so it will wobble or fall over.
-
Press play and all the cubes in the robot should move as one.
-
Finally, drag the robot object from the Hierarchy to the Project window to make a prefab out of the robot as well. You can now make many robots by dragging the prefab from the Project window to the Hierarchy. Feel free to do that as many times as you like.
Parenting can also get a bit funky. If we pick up a children of one object, we can move it wherever we want, BUT if we then move the parent object 5 units to the left, the child object will also do that, even though it’s not physically connected anymore.
- So let's grab one of the cubes inside the robot and move it somewhere, so that it's completely detached from the robot. Then pick the Robot from the Hierarchy and move it around. The disconnected cube should also move. Spooky.
Then, as a sidenote, there are also nested prefabs, which means prefabs within prefabs which combines the ideas of prefabs and parenting. We don’t need them right now, as the thinking around them can get pretty complex, but it’s good to understand that they exist. We’ll make one example of this to show that it’s possible, but it’s not expected to use them, just know that it might be something you encounter in a project.