void SetParent( GameObject value, bool keepWorldPosition )

robot_2Generated
code_blocksInput

Description

The SetParent method allows you to set a new parent for the current GameObject. This method is useful for organizing objects within a scene hierarchy, enabling you to change the parent-child relationships dynamically. The method takes two parameters:

  • value: The new parent GameObject to which the current object will be attached. If null, the object will be detached from its current parent and become a root object.
  • keepWorldPosition: A boolean value indicating whether the object should maintain its current world position when reparented. If true, the object's world position remains unchanged; if false, the object's local position relative to the new parent is recalculated.

Usage

To use the SetParent method, call it on an instance of a GameObject and pass the desired parent GameObject and a boolean indicating whether to keep the world position:

GameObject childObject = new GameObject();
GameObject newParent = new GameObject();

// Set newParent as the parent of childObject, keeping the world position
childObject.SetParent(newParent, true);

Example

GameObject childObject = new GameObject();
GameObject newParent = new GameObject();

// Set newParent as the parent of childObject, keeping the world position
childObject.SetParent(newParent, true);