I need help in my script

im making top down game , now i have player and enemy can follow player ,
but i wrote script to make enemy destroy player if he touch him ,and its not working (there is one error in ontiggerenter2d
and nothing is working)
i tried it without ontiggerenter2d in update and still dont working

using UnityEngine;
using System;
public class colliderscript : MonoBehaviour
{
public GameObject square;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{

}

// Update is called once per frame
void Update()
{
    OnTriggerEnter2D();
}
private void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "player")
    {
        Destroy (square);










    }
}

}

using UnityEngine;
public class colliderscript : MonoBehaviour
{

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "player")
    {
        Destroy(other.gameObject);
    }
}

}

nothing is working

You should check couple of things first to know if you have set the collider correctly or not.

  1. If the collider is attached to the same gameobject as this script or not
  2. check “is trigger” of the collider otherwise code will not work
  3. if your player gameobject has the “player” tag or not

Also can you post a screenshot of the error you are getting?

1 Like

thank you so much the problem was in intrigger u helped me to make my first game

1 Like