2d multiple falling objects

Hey guys,

I am trying to make a little game where objects fall from the top of the camera. When they are falling you can click on them to destroy them.

This is working well when i am using 10 objects but when i make 100 objects or let them fall in repeat mode the onMouseDown() event is not triggered.

I’m new to Unity and i was wondering if there is something i must be looking for to investigate what is the issue here.

I have the following :

  • A gameObject that has a script to make the game objects. I tried to make them in waves with WaitForSeconds, a loop that makes all the objects but then above each other or a invokeRepeating method with a delay.

  • My figures are made with a Prefab that has a script that uses the OnMouseDown() to destroy the gameObject. I also tried the normal gravity of a object and transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World); for consistent falling speed.
    I also tried the Raycast with :
    `if (Input.GetMouseButtonDown(0))
    {
    Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    RaycastHit2D hit = Physics2D.Raycast(worldPoint, new Vector3(0,0,1));

         Debug.Log("Click");
    
         if (hit.collider != null && hit.collider.gameObject.tag == "Figure")
         {
             Debug.Log("Destroy : " + hit.collider.name);
             Destroy(hit.collider.gameObject);
         }
     }`
    

So a small repeat. My problem is that with allot of objects that are falling the click of the mouse does not correctly trigger a destroy on different ways. Appreciate the help thanks :smiley:

How many frames per second are rendered when you have many objects? If you have low fps it could happen that you click between two frames…

don’t use that many objects. use polling. use 10 to 15 objects repet the same objects.
like

list fallObjects = new ();

void Start(){

for(int i = 0; i < 10; i++){

GameObject g = instatiate(“your prefab”);

g.setative(false);

fallObjects.add(g);

}

}

void Update(){

if(input.GetMouseButtonDown(0)){

GameObject g = GetObjectFromPool ();

// position this guy in your needs

}
}

GameObject GetObjectFromPool (){

for(int i = 0; i< fallObjects.count; i++){

if(! fallObjects .activeinhirachie){
fallObjects .setactive(true);
return fallObjects ;
}
}
GameObject g = instatiate(“your prefab”);
g.setative(true);
fallObjects.add(g);
return g;
}
// NOTE Don’t Forget to deactivate objects when they are out of camera

I followed one of the unity tutorials and there they made a Boundery over the whole background with a OnTriggerExit2d event. This made the game performance bad. I changed this to a smaller box collider under the camera with a onTriggerEnter2d to remove the objects.

I found the problem with the script from @Nkoeppel because my layers where not set up right yet and deleted my boundery. Then everything worked but my boundery collision. Then i fixed the boundery collision and the game worked allot better.

Thanks for the Help guys.