Manchmal wird die OnMouseDown () -Methode in Unity ausgeführt, manchmal nicht

So habe ich diesen Code an ein Quad angehängt.

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;
        }
    }
}

Das obige Skript generiert Spielobjekte zur Laufzeit, denen ich ein anderes Skript zugewiesen habe:

public class ShapeBehavior : MonoBehaviour {

    void OnMouseDown(){
        Debug.Log ("Destroy");
        Destroy (gameObject);
    }
}

Das Problem ist, manchmal wird die OnMouseDown () ausgeführt, manchmal nicht. Ich kann nicht herausfinden, warum und wie das Problem behoben werden kann.

Antworten auf die Frage(4)

Ihre Antwort auf die Frage