【发布时间】:2016-04-06 01:38:19
【问题描述】:
我正在使用 Cube 网格实例化一个新游戏对象。我希望它有一个 BoxCollider2D 组件。
我首先使用 GameObject.CreatePrimitive 用图元实例化一个新的 GameObject。 '使用原始网格渲染器和适当的碰撞器创建游戏对象。'
GameObject inst = GameObject.CreatePrimitive(PrimitiveType.Cube);
附加了一个默认的 BoxCollider (3D)。我不想要这个,所以我删除它。
Destroy(inst.GetComponent<Collider>());
这可行,在编辑器中我可以看到我现在有一个没有 BoxCollider 的立方体。
现在我尝试添加一个 BoxCollider2D
inst.AddComponent<BoxCollider2D>();
完整的方法是这样的
private GameObject createSnakeCube()
{
GameObject inst = GameObject.CreatePrimitive(PrimitiveType.Cube);
Destroy(inst.GetComponent<Collider>());
inst.AddComponent<BoxCollider2D>();
return inst;
}
但是。我将收到以下错误消息
Can't add component 'BoxCollider2D' to Cube because
it conflicts with the existing 'BoxCollider' derived component!
UnityEngine.GameObject:AddComponent()
有谁知道为什么 Unity 仍然认为 BoxCollider 附加在编辑器中不可见时?
【问题讨论】: