给名为parent的空对象添加子对象
如图所示
新建一个文件,名为addChildTest.cs
将该脚本文件添加到Parent上
将父对象添加到脚本上
代码如图所示
using UnityEngine;
using System.Collections;
public class AddChildTest : MonoBehaviour {
// Use this for initialization
public GameObject parentObj;
void Start () {
GameObject go = new GameObject("Haha");
go.transform.SetParent(parentObj.transform);
}
// Update is called once per frame
void Update () {
}
}
运行后,结果如下: