To create and use prefabsAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
See in Glossary in your project, you must do the following:
To create a prefab asset, perform the following steps:
Assets
folder in the Project windowA window that shows the contents of your Assets
folder (Project tab) More infoUnity then creates a prefab asset from the GameObject, all its components, and child GameObjects. The original GameObject becomes an instance of the newly created prefab asset.
Assets
folder, the Health Pack object is saved as a .prefab file, and the original GameObject becomes an instance of the prefab asset. The Inspector displays details of the Health Pack prefab asset.If you drag multiple GameObjects that aren’t already prefabs into the Project window, Unity creates new prefab assets for each one without any additional steps. However, if any of the GameObjects are existing prefabs or prefab variants, Unity displays a dialog which asks you to confirm whether you want to create new prefab assets or new variants from the GameObjects.
You can replace a prefab asset with a GameObject or prefab instance that’s in the Hierarchy window. To replace a prefab asset:
If the contents of the GameObject or prefab instance differ from the prefab asset, then Unity displays a dialog to confirm that you intend to replace the prefab asset.
Unity tries to preserve references to the prefab and the individual parts of the prefab such as child GameObjects and components. To do this, it matches the names of GameObjects between the new prefab and the existing prefab that you’re replacing.
Because Unity performs this matching by name only, if there are multiple GameObjects with the same name in the prefab asset’s hierarchy, the matching is unpredictable. Therefore, to ensure Unity preserves the references when saving over an existing prefab, make sure all GameObjects within the prefab have unique names.
Unity behaves in a similar way if a GameObject has more than one of the same type of component attached to it.
To create an instance of a prefab asset:
You can also create instances of prefabs at runtime using scripting. For more information, refer to Instantiating prefabs.
You can replace a prefab instance’s parent prefab asset, while retaining the instance’s position, rotation, and scale. Unity merges the contents of the new prefab asset and preserves any overrides and references via name-based matching.
You can replace a prefab asset in the following ways:
You can also use the PrefabUtility.ReplacePrefabAssetOfPrefabInstance
method to control this behavior in your scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.