void SetParent( GameObject value, bool keepWorldPosition )

robot_2Generated
code_blocksInput

Description

The SetParent method allows you to set the parent of a 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 GameObject that will become the new parent.
  • keepWorldPosition: A bool indicating whether the GameObject should maintain its current world position after the parent change. If set to true, the object's world position remains unchanged; if false, the object's position will be relative to the new parent.

Usage

To use the SetParent method, call it on an instance of a GameObject and pass the desired parent GameObject and a boolean value for keepWorldPosition. This will reparent the object, optionally maintaining its world position.

Example

// Example of using SetParent method
GameObject childObject = new GameObject();
GameObject newParentObject = new GameObject();

// Set newParentObject as the parent of childObject
// Keep the child's world position unchanged
childObject.SetParent(newParentObject, true);