My collision code for my player isn't working.

I have a player that is supposed to be destroyed when it touches some sphere’s but that code is not working.

Could someone please help, here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerdeath : MonoBehaviour
{
    void OnTriggerEnter(Collider collision)
    {
        if (collision.gameObject.CompareTag("Sphere1(Clone)")||
        collision.gameObject.CompareTag("Sphere2(Clone)")||
        collision.gameObject.CompareTag("Sphere3(Clone)")||
        collision.gameObject.CompareTag("Sphere4(Clone)"))
        {
            Destroy(collision.gameObject);
            Debug.Log("You dead");
	    }
    }
}

You are using OnTriggerEnter and not OnCollisionEnter.

On your player collider is the trigger is trigger ticked? This script is on the player, right? It might be that the trigger needs to be ticked on the objects that kill the player instead. I would also try and collide with all the spheres and see if the outcome is different.

Did you set the tag yourself to be those strings? Or do you assume the names of the objects are the tags? They are different notions. If you are using the CompareTag() method make sure you have set the tag value in the inspector to the correct value.


Also, stupid question on my side I know, but are you sure the sphere object has a Collider attached to it?