Prefabs

A prefab is a node tree saved as a .prefab file — the same format as a scene. Any scene can be used as a prefab, and any prefab can be opened and edited like a scene. The difference is only in how you use it: a scene is loaded, a prefab is instantiated into something already loaded.

Creating a prefab

  1. In the Assets panel, right-click a folder → New → Scene, and name it, for example Crate.
  2. Double-click it to open it in its own tab.
  3. Build the object: add nodes in the Scene Tree, components in the Inspector. Use Copy and Paste in the Scene Tree to bring nodes over from another open scene.
  4. Save with Ctrl+S.

Referencing a prefab from a component

Field type Use it when
AssetReference<Prefab> You only need the spawned node tree.
PrefabReference<T> You want a specific component T from the spawned copy.

Drag the .prefab from the Assets panel onto the field in the Inspector.

Instantiating

public class CrateSpawner : Component
{
    public PrefabReference<ModelComponent>? Crate;
    public int Count = 5;

    public override async void OnStart()
    {
        var scenes = RuntimeServices.TryGet<ISceneManager>();
        if (scenes is null || Crate is null) return;

        for (var i = 0; i < Count; i++)
        {
            var model = await Crate.InstantiateAsync(scenes, Node);
            model.Node!.Transform.Position = new Vector3(i * 1.5f, 0, 0);
        }
    }
}
Call Result
scenes.InstantiateAsync(assetReference, parent) Loads the prefab and adds a copy under parent (or the active scene's root when null). Returns the copy's root node.
scenes.InstantiateAsync(assetId, parent) The same, from an asset id.
prefabReference.InstantiateAsync(scenes, parent) The same, returning the T component found in the copy. Throws if the prefab has none.
scenes.InstantiateAsync(component, parent) Clones the node tree that owns a component already in a scene.

A copy's components wake up as soon as it is created and start on the next frame, like everything else in the scene.

Removing a copy

Deactivate it with node.IsActive = false, or detach it from its parent's Children — for instance to return it to a pool.

What prefabs do not do yet

Copies are independent snapshots. Turian 1.0 has no linked prefab instances placed in the editor, no per-instance overrides, and no propagation of prefab edits into copies that already exist. Nested prefabs work as plain nesting: a prefab can instantiate other prefabs from its own components at runtime.


← All docs Edit this page