Prefabs are reusableDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/facepunch/sbox-public/llms.txt
Use this file to discover all available pages before exploring further.
GameObject templates saved to disk as .prefab assets. Instead of rebuilding the same object — say, an enemy with a model, health component, and AI — every time you need it, you design it once as a prefab and spawn copies at will. This page explains what prefabs are in s&box, how to create them, how to instantiate them from code, and how prefab instances and nesting interact.
What is a prefab?
A prefab is a saved snapshot of aGameObject hierarchy stored in a PrefabFile asset (.prefab). It captures the object’s name, transform, components, and children. When you spawn a prefab at runtime, the engine clones the template into a live GameObject that you can modify without affecting the original asset.
Internally, the live copy is called a prefab instance. The instance knows which prefab it came from (PrefabInstanceSource) and tracks overrides via a diff/patch system, so editor changes to the prefab can be pushed to existing instances.
Creating a prefab
Prefabs are created and edited in the s&box scene editor:Build your GameObject
Set up the
GameObject in the scene — add components, configure properties, add children.Save as prefab
Right-click the
GameObject in the hierarchy and choose Save as Prefab. The editor writes a .prefab file to your project’s asset folder.Instantiating a prefab at runtime
UseGameObject.Clone to spawn a prefab. The static overloads accept a file path or a PrefabFile resource; the instance overload clones any existing GameObject.
Clone always returns a fully initialised GameObject. All components on the instance and its children will receive their OnAwake and OnEnabled calls in the same frame batch.
Cloning an existing GameObject
Clone is also available as an instance method. Call it on any live GameObject to create a deep copy, preserving the full component and child hierarchy.
Prefab instances
A cloned prefab is called a prefab instance. You can inspect this state at runtime:Breaking from a prefab
Once you callBreakFromPrefab(), the object loses its link to the source asset. It becomes a standalone GameObject. Editor overrides are no longer tracked and future changes to the prefab asset will not propagate.
Refreshing from the source
If you want to reset an instance back to the current state of its source prefab, callUpdateFromPrefab():
Nested prefabs
A prefab can contain other prefab instances as children — this is called nesting. The engine tracks nested instances separately so that overrides on the outer prefab do not corrupt the inner one.OuterEnemy.IsPrefabInstanceRoot→trueWeapon.IsNestedPrefabInstanceRoot→trueWeapon.OutermostPrefabInstanceRoot→OuterEnemy
Reading a prefab without cloning
If you need to inspect the prefab’sGameObject template without spawning it into the scene, use GetPrefab:
The object returned by
GetPrefab is the engine’s internal cache. Never modify it or add components to it; always call Clone to get a usable copy.Next steps
Scene and GameObject
Understand how the hierarchy, transforms, and enabling work.
Components
Add behaviour to your prefabs with the component lifecycle.