unity set parent code example
Example 1: setparent unity
using UnityEngine;public class ExampleClass : MonoBehaviour
{
public GameObject child; public Transform parent;
public void Example(Transform newParent)
{
child.transform.SetParent(newParent);
child.transform.SetParent(newParent, false);
child.transform.SetParent(null);
}
}
Example 2: unity change parent in script
player.transform.parent = newParent.transform;
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: set parent of gameobject unity
child.transform.SetParent(newParent);
Example 5: parent unity
public gameobject player
public gameobject Newparent
player.transform.parent = newParent.transform;
Example 6: how to get parent gameobject in unity
childObject.transform.parent.gameObject