【问题标题】:Destroying GameObject and Instantiating a Different GameObject销毁游戏对象并实例化不同的游戏对象
【发布时间】:2020-04-18 02:28:01
【问题描述】:
我有当立方体游戏对象与地形碰撞时销毁它的代码。但是,我不确定在立方体被销毁后在其位置实例化一个 New Sphere GameObject 之后我将如何处理。
这是当前代码:
{
void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.tag != "Destroy")
{
Destroy (gameObject);
}
}
}
【问题讨论】:
标签:
unity3d
instantiation
gameobject
【解决方案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);
}
}