So I have this code attached to a Quad.
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;
}
}
}
The script above generates Game Objects at run time, to which I assigned another script:
public class ShapeBehavior : MonoBehaviour {
void OnMouseDown(){
Debug.Log ("Destroy");
Destroy (gameObject);
}
}
The problem is, sometimes the OnMouseDown() executes, sometimes it does not. I cannot figure out why , and how to fix it.