menu_bookDocumentation
GameObject.Clone: Two-Pass Cloning, CloneConfig, NotCloned Flag, and Reference Rewiring
GameObject.Clone: Spawning Prefabs and Cloning GameObjects with CloneConfig
GameObject.Clone is the primary way to instantiate prefabs and duplicate GameObjects in s&box. It performs a deep copy, rewiring all internal GUID references so the clone is fully independent.Cloning a Prefab from Path
CSHARP
// Clone a prefab at a world position
var go = GameObject.Clone( "prefabs/my_prefab.prefab", new Transform( position, rotation ) );
// Clone with full config
var go = GameObject.Clone( "prefabs/my_prefab.prefab", new CloneConfig
{
Transform = new Transform( position, rotation ),
Parent = parentGameObject,
StartEnabled = true,
Name = "My Instance"
} );
// Clone from a PrefabFile resource
var prefab = ResourceLibrary.Get<PrefabFile>( "prefabs/my_prefab.prefab" );
var go = GameObject.Clone( prefab, new Transform( position ) );Cloning an Existing GameObject
CSHARP
// Clone at origin
var clone = someGameObject.Clone();
// Clone at position
var clone = someGameObject.Clone( position );
// Clone at position + rotation
var clone = someGameObject.Clone( position, rotation );
// Clone with parent
var clone = someGameObject.Clone( new Transform( position ), parent: parentGo );CloneConfig
CSHARP
var config = new CloneConfig
{
Transform = new Transform( position, rotation, scale ),
Parent = parentGameObject, // null = scene root
StartEnabled = true, // whether to enable after cloning
Name = "Override Name" // null = use original name
};How Clone Works Internally
Clone is a two-pass process:
- InitClone — creates all GameObject and Component instances, building an originalToClonedObject mapping
- PostClone — copies all component properties, rewiring references using the mapping
This two-pass approach ensures that cross-references within the hierarchy (e.g., a component property pointing to another component in the same hierarchy) are correctly rewired to point to the cloned versions.
ComponentFlags.NotCloned
Components with ComponentFlags.NotCloned are skipped during cloning:
CSHARP
Flags |= ComponentFlags.NotCloned; // this component won't be included in clonesGameObjectFlags.NotSaved
Child GameObjects with GameObjectFlags.NotSaved are also skipped during cloning.
Scale Behavior
When cloning a non-prefab GameObject, the clone's scale is cloneConfig.Transform.Scale original.LocalScale. When cloning a PrefabScene, the full transform (position, rotation, scale) is composed with the prefab's local transform.
Was this helpful?