I have a game object ‘Crowd’ that is instantiated in the scene multiple times. Each time it instantiates, it creates a clone called ‘Crowd(Clone)’ in the scene. I wish to destroy this game object each time it moves out of the scene bounds. For this, I created a blank game object and placed it outside the area where the camera cant see and I put a script on this game object to Destroy each copy of the instantiated object if it collides with this destroyer. The following is the script that I have attached to this destroyer.
using UnityEngine;
using System.Collections;
public class SpawnKill : MonoBehaviour {
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "Crowd(Clone)")
{
Debug.Log("Hit");
Destroy(collision.gameObject);
}
}
}
Both the game objects, ‘Crowd’ as well as the destroyer, have a Box Collider 2D components. I tried turning on the ‘Is Trigger’ checkbox, as well as rename the collision.gameObject.name from ‘Crowd(Clone)’ to ‘Crowd’ but it just wouldnt detect.
For some reason, the collisions are just not detecting and I don’t know why. I’m using the 2D interface in Unity.
Any ideas anyone ?