销毁GameObject并实例化另一个GameObject

时间:2020-04-18 02:28:01

标签: unity3d instantiation gameobject

我有用于在与地形碰撞时销毁Cube GameObject的代码。但是,我不确定在销毁多维数据集之后将New Sphere GameObject实例化后如何处理。

这是当前代码:

{
void OnCollisionEnter(Collision collision)
{
    if (collision.collider.gameObject.tag != "Destroy") 
    {
        Destroy (gameObject);
    }
}

}

1 个答案:

答案 0 :(得分:1)

1)将此脚本附加到地形游戏对象而不是立方体。

2)在编辑器中为多维数据集对象(例如多维数据集)添加一个新的标签

3)创建一个新的 sphere prefab 实例,您可以通过包含OnCollisionEnter()事件的脚本来访问该实例。

        void OnCollisionEnter(Collision collision)
        {
           if (collision.collider.gameObject.tag == "Cube")
           {
             //store the transform component of the gameobject to be destroyed.
             var transf = collision.gameObject.transform;

             //Destroy the collided gameobject
             DestroyImmediate(gameObject);

             //Instantiate in the position and rotation of the destroyed object.
             Instantiate(sphere, transf.position, transf.rotation);
           }
        }