Last modified by Maarten Struijk Wilbrink on 2021/11/08 18:01


Prefabs and prefab variants are an excellent way to use assets in your Scenes which contain broad similarities.

Especially later during development, prefabs can help you quickly create new instances of commonly used objects, or to change many simultaneously. Consider for example the following, where no prefabs are used:Prefabs_None

In the above image we have tree cubes. If we want to have another one of the same gameobject elsewhere (e.g. in a different Scene), we’d manually have to copy and paste this. Things are also becoming much more complex if we want to change some aspects of all those gameobjects. We’d have to remember to make those changes across all instances of the gameobject, and this can be cumbersome if many copies float around our project. 

You can create a Prefab from that first gameobject by dragging the gameobject from the Hierarchy to a Project folder, where it turns blue. You can now delete all Cube instances from the Hierarchy, thus removing them (temporarily) from the Scene:


To change your Prefab, locate the Prefab where you stored it in the Project folder, and click “Open Prefab” in the Inspector:


This takes you to the Prefab Mode:


You can now make the changes you want. Once done, exit Prefab Mode with the tiny back arrow....


To get the Prefabs back in the Scene you’ll have to drag one (or multiple) copies into the Scene (or Hierarchy). Each of those instantiated Prefabs will show the changes you made earlier in Prefab Mode. Notice that the instances in the Hierarchy also have a blue icon.


You can use this to create instances of those Prefabs throughout your project. 

Prefab Variants

Prefab Variants offer you even more flexibility. Variants allow you to keep multiple Prefabs identical to each other broadly speaking, while having individual differences. For instance: you have a Virtual Character, but want to have multiple versions, which are wearing different color clothing. To do this, you can create a Base Prefab using the steps above. Then, in the Project view, right-click on the Base, and click on “Create” — “Create Prefab Variant”. 


Variants will have a shaded side on their icon, whilst the Base prefab will have a solid blue icon.

Create as many Variants as you want. Now you can make changes to that Variant, again in Prefab Mode. The changes will only be reflected by that Variant.


Put the Variants into the scene as well.


With the Green variant, I changed both the Material (color), and the Scale (double the size) inside of Prefab Mode.

Not shown in the images above, but you can drag multiple instances of your Variants into the Scene as well, if you'd like to see more green cubes for instance. 

If you want to make changes that are reflected by (almost) all Prefab versions, you can open the Base Prefab and make the changes from there. These changes will be pushed to all (/most) derived Variants. 


(Small side-note here, I'm doing a bit of a no-no here: it's usually not good practice to do non-uniform scaling... you usually want to keep the X,Y,Z of the Scale in the Transform identical to each other. This is mainly because child objects will inherit this non-uniform scale, which can cause weird issues. This time however I chose to do non-uniform scaling for clarity of image).

If you go back to the Scene, it looks like this:


In the above image you can see that all base Prefabs (red), but only one of the Variants (blue) have the new scale. This is because earlier we set the scale of the Green variant separately! If you change an aspect of a Variant explicitly, those changes are not overwritten by a change in the Base. 

Caveats and possibilities

Variants can have more or fewer components on each GameObject: you can add or remove scripts etc to your heart's content. However, you cannot restructure the order of the GameObjects of the Variants freely (for instance if you'd have multiple child game objects on your prefab). From the Variants perspective, this aspect is closely coupled to the Base, so you'd have to make those kinds of changes at the Base (and have them be pushed to all Variants). Keep this in mind when structuring your Prefabs. 

Further flexibility can be had by making Variants of Variants (or, if you feel bold: Variants of Variants of Variants of …).

Another option is to nest Prefabs (Variants). This way you can have one Prefab be easily changed, whilst being part of another Prefab. In the image below you can see that the XR Rig Prefab has a nested Locomotion System Prefab inside:


Instantiating Prefabs && Contact them through your code

Another benefit of creating Prefabs is that you can spawn (Instantiate) and remove (Destroy) them during runtime. This allows you to only have the required Game Objects in your Hierarchy. You can also use it to instantiate multiples of the same Game Object, or to create networked versions of those game objects. 

One main obstacle when creating objects during runtime is to be able to connect the code and components on the instantiated objects to the rest of your code-base. Many solutions to this situation exist, and a few are discussed here

XWiki 14.10.13