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.