Answers for "unity set parent canvas"


unity set parent canvas

static void Switch()
    //Create new GameObject
    GameObject go = new GameObject("switch");

    //Find Canvas in the Scene
    Canvas canvas = (Canvas)GameObject.FindObjectOfType(typeof(Canvas));

    //Get Canvas GameObject
    GameObject canvasGameObject = canvas.gameObject;

    //Make the new GameObject child of the Canvas
    go.transform.parent = canvasGameObject.transform;
    go.transform.localPosition =;

    //Change the new GameObject Layer to UI
    go.layer = 5; //Or go.layer = canvasGameObject.layer;

    //Add Rect Transform to it
Posted by: Guest on October-25-2020

C# Answers by Framework

Browse Popular Code Answers by Language