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, allowing you to change the parent-child relationship between GameObject instances.

Usage

To use the SetParent method, call it on a GameObject instance, passing in the new parent GameObject and a boolean indicating whether to keep the world position unchanged.

  • value: The GameObject that will become the new parent.
  • keepWorldPosition: A boolean value. If set to true, the GameObject will maintain its current world position after the parent is changed. If false, the GameObject will adopt a new position relative to the new parent.

Example

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

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

// Alternatively, set newParent as the parent and adjust position relative to new parent
childObject.SetParent(newParent, false);