因此,我将此代码附加到Quad。有时Unity中的OnMouseDown()方法执行,有时它不会
public class ShapeGrid : MonoBehaviour {
public GameObject[] shapes;
void Start(){
GameObject[,] shapeGrid = new GameObject[3,3];
StartCoroutine(UpdateGrid());
}
IEnumerator UpdateGrid(){
while (true) {
SetGrid();
yield return new WaitForSeconds(2);
}
}
void SetGrid(){
int col = 3, row = 3;
for (int y = 0; y < row; y++) {
for (int x = 0; x < col; x++) {
int shapeId = (int)Random.Range (0, 4.9999f);
GameObject shape = Instantiate (shapes[shapeId]);
shape.AddComponent<ShapeBehavior>();
Vector3 pos = shapes [shapeId].transform.position;
pos.x = (float)x*3;
pos.y = (float)y*3;
shapes [shapeId].transform.position = pos;
}
}
}
上面的脚本在运行时生成的游戏对象,向其中我分配另一个脚本:
public class ShapeBehavior : MonoBehaviour {
void OnMouseDown(){
Debug.Log ("Destroy");
Destroy (gameObject);
}
}
的问题是,有时onmousedown事件()执行,有时它没有。我无法弄清楚为什么,以及如何解决它。
ShapeGrid类中的形状已经有一个碰撞器。因此,如果我在每个克隆的游戏对象上添加碰撞器,我都会收到此错误:无法将组件'BoxCollider'添加到圆圈(克隆),因为它与现有的'BoxCollider2D'派生组件冲突! – jeanl
你说“有时候”,你可以检查问题是否存在于同一个对象上,或者是这个随机的东西吗?你也可以检查图层,对象不能在忽略Raycast图层上。 –
一直试图找到一种模式。我认为这是随机的。我不认为我使用Raycasts – jeanl