英文:
C# object destroying its parent
问题
我正在使用Unity制作一个2D游戏。我有一个物体,它会摧毁一切接触到的东西。当我启动游戏时,这个物体立即摧毁了它自己的父物体和自身。我希望它在碰撞时摧毁其他物体,但不包括它的父物体。有没有办法实现这个?(是否有类似"isParent"的东西?)
以下是代码部分:
if (other.CompareTag("Room"))
{
Destroy(other.gameObject);
Debug.Log("Room Destroyed");
}
英文:
I am making a 2D game in Unity. I have an object, which destroys everything it touches. When I start the game, the object instantly destroys its parent and itself. I would like it to destroy objects on collision, but not if object is its parent. Is there a way to do it? (isnt there something like isParent?)
Code is here:
if (other.CompareTag("Room"))
{
Destroy(other.gameObject);
Debug.Log("Room Destroyed");
}
答案1
得分: 1
if (other.CompareTag("Room") && transform.parent != other.gameObject.transform)
{
Destroy(other.gameObject);
}
Though, you might want to check if other
is anywhere in this object's hierarchy, rather than just checking to see if it's the immediate parent.
英文:
if (other.CompareTag("Room") && transform.parent != other.gameObject.transform)
{
Destroy(other.gameObject);
}
Though, you might want to check if other
is anywhere in this object's hierarchy, rather than just checking to see if it's the immediate parent.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论