menu_bookDocumentation
Prefabs: Reusable GameObjects with Runtime Spawning via Clone
A prefab is a GameObject that can be used in multiple places — across scenes or instantiated at runtime.
Assets
Prefabs are saved as PrefabFile assets. To create one, right-click a GameObject in the scene and select "Convert to Prefab". When updated, all instances in scenes update too.
In Scene
Prefab instances appear blue in the hierarchy. They can't be edited directly — right-click and choose "Unlink from Prefab" to convert to normal GameObjects.
Spawning in Code
A GameObject property on your Component can reference a PrefabFile. Use .Clone() to instantiate:
CSHARP
public sealed class MyGun : Component
{
[Property]
GameObject BulletPrefab { get; set; }
protected override void OnUpdate()
{
Assert.NotNull( BulletPrefab );
if ( Input.Pressed( "Attack1" ) )
{
GameObject bullet = BulletPrefab.Clone( WorldPosition );
// bullet is now in the scene - get components, set velocity, etc.
}
}
}Call bullet.BreakFromPrefab() to remove the prefab link and have it appear as normal GameObjects.
Was this helpful?