Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


SetParent

Set the parent of the transform.
Read time 3 minutesLast updated 4 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

SetParent(TransformHandle)

Set the parent of the transform.
public void SetParent(TransformHandle p)

Parameters

Remarks

This method is the same as the TransformHandle.parent property except that it also lets the TransformHandle keep its local orientation rather than its global orientation. This means for example, if the GameObject was previously next to its parent, setting
worldPositionStays
to false will move the GameObject to be positioned next to its new parent in the same way.
The default value of
worldPositionStays
argument is true.
The following images are of a scene with three GameObjects: a new parent cube, a parent sphere, and a child sphere.
TransformSetParentOriginal (SetParent)
The new parent cube is on the left of the screen and the child sphere is in its original position, next to the parent sphere on the right of the screen.
TransformSetParentWorldTrue (SetParent)
After calling
SetParent
with
worldPositionStays
set to true, all objects are in the same position as their original positions.
TransformSetParentWorldFalse (SetParent)
After calling
SetParent
with
worldPositionStays
set to false, the child sphere is in the same position but is now relative to the new parent cube instead.

Examples

using UnityEngine;public class ExampleClass : MonoBehaviour{ public GameObject child; public TransformHandle parent; //Invoked when a button is clicked. public void Example(TransformHandle newParent) { // Sets "newParent" as the new parent of the child GameObject. child.transformHandle.SetParent(newParent); // Same as above, except worldPositionStays set to false // makes the child keep its local orientation rather than // its global orientation. child.transformHandle.SetParent(newParent, false); // Setting the parent to ‘TransformHandle.None’ unparents the GameObject // and turns child into a top-level object in the hierarchy child.transformHandle.SetParent(TransformHandle.None); }}

SetParent(TransformHandle, bool)

Set the parent of the transform.
public void SetParent(TransformHandle parent, bool worldPositionStays)

Parameters

The parent TransformHandle to use.

worldPositionStays

If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before.

Remarks

This method is the same as the TransformHandle.parent property except that it also lets the TransformHandle keep its local orientation rather than its global orientation. This means for example, if the GameObject was previously next to its parent, setting
worldPositionStays
to false will move the GameObject to be positioned next to its new parent in the same way.
The default value of
worldPositionStays
argument is true.
The following images are of a scene with three GameObjects: a new parent cube, a parent sphere, and a child sphere.
TransformSetParentOriginal (SetParent)
The new parent cube is on the left of the screen and the child sphere is in its original position, next to the parent sphere on the right of the screen.
TransformSetParentWorldTrue (SetParent)
After calling
SetParent
with
worldPositionStays
set to true, all objects are in the same position as their original positions.
TransformSetParentWorldFalse (SetParent)
After calling
SetParent
with
worldPositionStays
set to false, the child sphere is in the same position but is now relative to the new parent cube instead.

Examples

using UnityEngine;public class ExampleClass : MonoBehaviour{ public GameObject child; public TransformHandle parent; //Invoked when a button is clicked. public void Example(TransformHandle newParent) { // Sets "newParent" as the new parent of the child GameObject. child.transformHandle.SetParent(newParent); // Same as above, except worldPositionStays set to false // makes the child keep its local orientation rather than // its global orientation. child.transformHandle.SetParent(newParent, false); // Setting the parent to ‘TransformHandle.None’ unparents the GameObject // and turns child into a top-level object in the hierarchy child.transformHandle.SetParent(TransformHandle.None); }}