🤔

Referencing Prefabs from Scenes#

You can only reference components on the root (the top-level GameObject) of a prefab. See references to prefab children if you want to reference prefab sub-objects.

Implementation#

1. Add or choose a root component on your prefab#

On the top-level GameObject of your prefab asset, add or choose a component that you want to reference.

error

This example uses PrefabComponentType, it will need to be replaced with the root component type you chose.

Your spawner will clone the whole prefab and you will end up with a reference to the copy of the root component.

2. On your spawner, expose a serialized reference to your root component#

The field must be marked with SerializeField:

[SerializeField] private PrefabComponentType _prefab;

or can be public:

public PrefabComponentType Prefab;

3. Reference the prefab asset in the spawner's Inspector#

Drag the prefab asset into the slot or you will get an UnassignedReferenceException.

Dragging a prefab from the Project window into the field will reference the first matching Component found on the root of the object.

4. Instance the prefab asset from your spawner#

Create a variable for your new instance, and assign it the result of calling Instantiate with the prefab you declared earlier.

// A reference to our in-Scene clone of the prefab.
private PrefabComponentType _instance;

void Spawn()
{
    // Clone the prefab and assign it to our variable.
    _instance = Instantiate(_prefab);
}

5. Access the member you care about through your new instance#

public members on the root component can be accessed via the instance.

// Variables and properties
var variable = _instance.Variable;
_instance.Variable = variable;

// Methods
_instance.Method();
information

If you don't have autocomplete, configure your IDE to easily find member names and get error highlighting.

Notes#

Serialization and instancing#

When cloning a component using Instantiate, all attached objects and components are cloned with their properties set like those of the original object. Serialized references are locally maintained when Objects are instanced; references in prefab instances will be local, not referring to the original. Without indirection it's not possible for a prefab instance to maintain a reference to the prefab it was spawned from.

Prefabs and runtime#

Prefabs are an editor-only concept. At runtime prefab instances are not logically connected to what spawned them, and nor is any information about prefab hierarchies or overrides maintained.

Prefab roots#

The prefab root is the top-level GameObject of a prefab. You cannot directly reference the children of a prefab from the scene, so prefab children should generally reference a component on the root if they want data you've configured from the scene. Also see references to prefab children to learn more.

Referencing Components, not GameObjects#

Don't reference a GameObject unless you only use its methods. Referencing a component directly avoids using GetComponent.

/* --- 🟠 Wasteful and error-prone --- */
[SerializeField] private GameObject _prefab;

void Spawn()
{
    GameObject instance = Instantiate(_prefab);
    // GetComponent is required to do anything beyond calling SetActive.
    instance.GetComponent<Example>().Foo();
}

/* --- 🟢 Simple and communicates usage --- */
[SerializeField] private Example _prefab;

void Spawn()
{
    Example instance = Instantiate(_prefab);
    // Has direct access to useful methods.
    instance.Foo();
}