Collision detection not working for 2d game

I’m building a 2d game which is called Greek Runners where 2 people go head to head split screen with rocks going towards them.

The whole game is done but the lives system which isn’t working because of the collisions ill attach photos of the Rigidbody and Collider properties for both game objects (the rock and player) and the script I’m using:

Script:

using UnityEngine;

public class DisableOnCollisionWithTargets : MonoBehaviour
{
    public GameObject target1; // Assign the first target GameObject in the Inspector
    public GameObject target2; // Assign the second target GameObject in the Inspector

    private void OnCollisionEnter(Collision collision)
    {
        Debug.Log("Collision detected with: " + collision.gameObject.name);

        // Check if the object collided with target1 or target2
        if (collision.gameObject == target1 || collision.gameObject == target2)
        {
            Debug.Log("Collided with one of the targets. Disabling GameObject.");
            gameObject.SetActive(false);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("Trigger detected with: " + other.gameObject.name);

        // Check if the object collided with target1 or target2
        if (other.gameObject == target1 || other.gameObject == target2)
        {
            Debug.Log("Triggered by one of the targets. Disabling GameObject.");
            gameObject.SetActive(false);
        }
    }
}

I need this in a couple of hours so any help is appreciated

Thank you.

You are using OnCollisionEnter and OnTriggerEnter instead of OnCollisionEnter2D and OnTriggerEnter2D

1 Like