Search code examples
c#unity-game-enginetriggerscollisionvoid

i made a OnTriggerEnter2D void but not work


hi i made a OnTriggerEnter2D void but not work

i added rigidbody2d in player how will trigger the shop

and there is a box collider and is trigger in player and shop

but not work

using System.Collections.Generic;
using UnityEngine;

public class OpenShop : MonoBehaviour
{
    public GameObject ShopGui;
    public bool test = false;


    void Update()
    {
        if (test == true)
        {
            Destroy(gameObject);
        }
    }

    public void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.transform.tag == "Shop")
        {
            test = true;
        }
    }
}

shop properties

player properties


Solution

  • Remove the first BoxCollider on Player and keep the second one that has Is Trigger Enabled. Use this code ON THE SHOP

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            test = true;
        }
    }