Prefabs - s&box Documentation
Prefabs
Prefabs are reusable GameObject templates that allow you to create consistent, repeatable objects across your game. You create a prefab asset from a GameObject in your scene, then instantiate it multiple times. Changes to the prefab asset propagate to all instances, while individual instances can have overrides.
Creating Prefabs
In Editor
- Select the GameObject you want to make into a prefab
- Right-click and select Create Prefab From Selection
- Choose a location and name for your prefab file
- The original GameObject becomes an instance of the prefab
Prefab Assets
Prefabs are saved as .prefab files in your project. They appear in the Asset Browser and can be organized into folders like any other asset.
Instantiating Prefabs
In Code
// Instantiate a prefab into the scene
var instance = myPrefab.Clone();
instance.WorldPosition = spawnPosition;
// Or clone with a specific parent transform
var instance = myPrefab.Clone( parentTransform );
// Load and instantiate in one line
var enemy = Resource.Load<PrefabFile>( "prefabs/enemy.prefab" ).Clone();Runtime Prefab Creation
You can also create prefabs at runtime from existing GameObjects:
// Create a prefab from an existing GameObject at runtime
var runtimePrefab = new PrefabFile();
runtimePrefab.UpdateFrom( sourceGameObject );
// Then instantiate it multiple times
var copy1 = runtimePrefab.Clone();
var copy2 = runtimePrefab.Clone();Prefab Variants
Prefabs support variants that override specific properties while keeping the base prefab connection. This is useful for creating variations of enemies, items, or environmental objects.
- Right-click a prefab in the Asset Browser
- Select Create Variant
- Modify properties on the variant without affecting the base prefab
Instance Overrides
When you have a prefab instance in a scene, you can override specific properties:
- Select the prefab instance
- Modify properties in the Inspector
- The modified values show as bold (indicating overridden)
- Right-click properties to revert or apply overrides to the prefab asset