Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

var parent: Transform;
Transform parent;
parent as Transform

Description

The parent of the transform.

Changing the parent will modify the parent-relative position, scale and rotation but keep the world space position, rotation and scale the same.

	// Makes the camera follow this object by
	// making it a child of this transform.

// Get the transform of the camera var cameraTransform = Camera.main.transform;

// make it a child of the current object cameraTransform.parent = transform;

// place it behind the current object cameraTransform.localPosition = -Vector3.forward * 5; // make it point towards the object cameraTransform.LookAt(transform);

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Transform cameraTransform = Camera.main.transform;
    void Example() {
        cameraTransform.parent = transform;
        cameraTransform.localPosition = -Vector3.forward * 5;
        cameraTransform.LookAt(transform);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public cameraTransform as Transform = Camera.main.transform

	def Example() as void:
		cameraTransform.parent = transform
		cameraTransform.localPosition = ((-Vector3.forward) * 5)
		cameraTransform.LookAt(transform)

Another example:

	// Detaches the transform from its parent.
	transform.parent = null;
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Example() {
        transform.parent = null;
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Example() as void:
		transform.parent = null