在游戏中,您控制一个球体 (Sphere) 和两种掉落的盒子:deathCube 和 goldCube。当 Sphere 击中 DeathCube 时,Sphere 被摧毁,但它没有被摧毁,我不知道为什么。这些立方体是预制件,它们有一个标签(DeathCube、GoldCube)。
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "DeathCube")
{
Destroy (gameObject);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "GoldCube")
{
gold++;
}
}
如果 Sphere 击中 goldCube,您将获得分数,但这也不起作用。
最佳答案
尝试将两个 OnTriggerEnter 合并为一个。
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "DeathCube")
{
Destroy (gameObject);
}
if (other.gameObject.tag == "GoldCube")
{
gold++;
}
}
我相信第二个会覆盖第一个,绝不允许调用 Destroy()
。我本以为编译器会因此抛出错误,但您似乎没有指出这一点。
关于c# - Unity - 游戏对象在击中预制件时不会被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49617089/