Can't destroy square after meeting with rectangle

I was making a simple game. It is a game about clicking to shoot squares at a rectangle and the squares get destroyed after touching the rectangle but the squares don’t get destroyed

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D coll)
    {
         if(coll.GameObject.FindWithTag("Player"))
         {
             Destroy(gameObject);
         }
    }
    public Rigidbody projectile;
    public float speed = 4;
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Rigidbody p = Instantiate(projectile, transform.position, transform.rotation);
            p.velocity = transform.forward * speed;
        }
    }
 
    }

I do not know what is wrong because I am a beginner

My guess is that the squares you instantiated aren’t tagged as “Player”, therefore it won’t call Destroy().

Use Debug.Log("Hit!"); right below GameObject.FindWithTag() to see if the Destroy method is called at all.

Narrow down your problem and you’ll find the solution.

1 Like

I tried it again and got a error saying that Collision2D does not contain a definition for GameObject

It should be “gameObject”, with a lowercase ‘g’.

1 Like

Here’s the API reference: Collision2D.