title: Sometimes OnMouseDown() method in Unity executes, sometimes it does not
body: <p>So I have this code attached to a Quad.</p>
  
  <pre><code>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 &lt; row; y++) {
          for (int x = 0; x &lt; col; x++) {
              int shapeId = (int)Random.Range (0, 4.9999f);
              GameObject shape = Instantiate (shapes[shapeId]);
              shape.AddComponent&lt;ShapeBehavior&gt;();
              Vector3 pos = shapes [shapeId].transform.position;
              pos.x = (float)x*3;
              pos.y = (float)y*3;
              shapes [shapeId].transform.position = pos;
          }
      }
  }
  </code></pre>
  
  <p>The script above generates Game Objects at run time, to which I assigned another script:</p>
  
  <pre><code>public class ShapeBehavior : MonoBehaviour {
  
      void OnMouseDown(){
          Debug.Log ("Destroy");
          Destroy (gameObject);
      }
  }
  </code></pre>
  
  <p>The problem is, sometimes the OnMouseDown() executes, sometimes it does not. I cannot figure out why , and how to fix it.</p>