Instantiating scenes

Every scene is also a prefab: a .prefab file is a node tree you can open and edit on its own, and stamp into another scene as many times as you like. Author one enemy, one pickup or one room, then spawn it wherever the game needs it.

Referencing a prefab

Give your component a field of type AssetReference<Prefab>, or PrefabReference<T> when you want a specific component from the spawned copy:

namespace Usercode;

public class Spawner : Component
{
    public AssetReference<Prefab>? Enemy;
    public PrefabReference<LightComponent>? Lamp;
}

In the Inspector, drag the .prefab file from the Assets panel onto the field.

Spawning at runtime

Instantiation goes through the scene manager, which scripts get from RuntimeServices:

public class Spawner : Component
{
    public AssetReference<Prefab>? Enemy;
    public PrefabReference<LightComponent>? Lamp;
    ISceneManager? scenes;

    public override void OnAwake() => scenes = RuntimeServices.TryGet<ISceneManager>();

    public override async void OnStart()
    {
        if (scenes is null || Enemy is null || Lamp is null) return;

        // Adds a copy under this node; pass null to add it to the active scene's root.
        var enemy = await scenes.InstantiateAsync(Enemy, Node);
        enemy.Transform.Position = new Vector3(2, 0, 0);

        // Returns the LightComponent found in the spawned copy.
        var lamp = await Lamp.InstantiateAsync(scenes);
        lamp.Intensity = 3f;
    }
}
  • InstantiateAsync(reference, parent) loads the prefab, copies it and attaches the copy under parent, or under the active scene's root when parent is null.
  • PrefabReference<T>.InstantiateAsync(scenes) does the same and returns the T component of the copy.
  • InstantiateAsync(component) clones the hierarchy that owns a component already in the scene.

Each copy is independent: once spawned, changing it does not affect the prefab or other copies.

Editing a prefab

Double-click the .prefab in the Assets panel to open it in its own tab, change it and save. Every copy spawned afterwards reflects the change. To move nodes between open scenes, use Copy and Paste in the Scene Tree.

Not yet available

Linked prefab instances placed in the editor, per-instance overrides and nested-prefab propagation are not part of Turian 1.0. Instantiation today is a copy made at runtime.


← All docs Edit this page