Hi, Let’s Imagine player shoots gameObjects with different tags. Let that be “Square”, “Hex”, and “Rectangle”. When player shoots gameObject with tag “Square” and hits another gameObject with the same tag “Square”, from previous shoots, I want to destroy previous gameObject and have the one which has been recently shot remain. Problem is when gameObject hits the another gameObject with the same tag, an event is fired twice and this makes both gameObjects disappear. Code I am using allows only one occurrence, so one GameObject will in fact get destroyed, but when player hits more than one GameObject with the same tag “Square”, those will remain, and I want them to be destroyed as well, so in the end only remains the one that was shot lastly, and all gameObjects with the same tag “Square” that were hit in that particular shot get destroyed. Do you have any suggestions how can I do this?
private void OnCollisionEnter2D(Collision2D collision)
{
if (ignoreCollision) return;
Invoke("Landed", 1f); // creates new gameObject for the player to shoot
Debug.Log(gameObject.tag + " collided with " + collision.gameObject.tag);
ignoreCollision = true;
if (collision.gameObject.tag == gameObject.tag)
{
Destroy(collision.gameObject);
}
}