unity get parent transform code example
Example 1: unity reference parent gameobject
transform.parent.gameObject
Example 2: transform object according to its parent unity
transform.localPosition = new Vector3(12.5f, 12.5f, 12.5f);
Example 3: c# unity parent object
using UnityEngine;
using System.Collections;public class ExampleClass : MonoBehaviour
{
public GameObject player;
public void SetParent(GameObject newParent)
{
player.transform.parent = newParent.transform;
Debug.Log("Player's Parent: " + player.transform.parent.name);
if (newParent.transform.parent != null)
{
Debug.Log("Player's Grand parent: " + player.transform.parent.parent.name);
}
} public void DetachFromParent()
{
transform.parent = null;
}
}
Example 4: how to parent something unity
public GameObject GameObjectYouWantToParent;
public GameObject TheNewParent;
Transform GameObjectsTransform = GameObjectYouWantToParent.transform;
GameObjectsTransform.parent = TheNewParent;