Unity3D:实例化创建 NullReferenceException

Unity3D: Instantiate creates NullReferenceException

Transform child = GameObject.FindWithTag("state").transform;
    child.name = first_guy+""; child.tag = first_guy+"";
    GameObject  child_dup = Instantiate( child, new Vector3(0,0,0),Quaternion.identity) as GameObject; 
    Debug.Log (child_dup.name);

上面的代码给我错误,"NullReferenceException: Object reference not set to an instance of an object"。 这是因为我正在尝试实例化局部变量吗?如何在 运行 时间内复制一个游戏对象?

您的问题是您正在尝试实例化一个 Transform 这是一个组件,无法实例化,因为它必须附加到 GameObject。

Instantiate( child.gameObject, ...)