set parent unity 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: set parent of gameobject unity
child.transform.SetParent(newParent);
Example 3: parent unity
public gameobject player
public gameobject Newparent
player.transform.parent = newParent.transform;
Example 4: unity set parent canvas
[MenuItem("GameObject/UI/Switch")]
static void Switch()
{
GameObject go = new GameObject("switch");
Canvas canvas = (Canvas)GameObject.FindObjectOfType(typeof(Canvas));
GameObject canvasGameObject = canvas.gameObject;
go.transform.parent = canvasGameObject.transform;
go.transform.localPosition = Vector3.zero;
go.layer = 5;
go.AddComponent<RectTransform>();
}