2
votes

I am trying to make the game go to Dead scene when collided by does not work. And it even does not detect the collsion.

I've attached the script to empty game object that has box collider with is trigger ticked and the player does have a Rigidbody too.

I am not sure what is wrong, please help.

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

public class Health : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnCollisionEnter(Collision collision)
    {
        Debug.Log("dead without if statement");
        if (collision.gameObject.tag == "Player")
        {
            Debug.Log("Dead Mate");
            SceneManager.LoadScene("DeadScreen");
        }
    }
}
1
Probably the class "Health" Does not have a collider. Your Player Class may be the one that collides and you should detect collisions using the Player class instead of Health Class.YouneS
Try to put a collider into the player gameobject, and did you put the tag "Player" into the player gameobject? I think that these are the 2 errors that you can have with the information that you have given.DomCR
I agree with YouneS. You should separate your concerns: detecting the collision and handling the collisionEliasar
For collision, check Unity's Colliders page and match your two object's properties to find whether or not a collision would be detected.Eliasar

1 Answers

1
votes

"I've attached the script to empty game object that has box collider with is trigger ticked and the player does have a Rigidbody too." If i understand you correct you enabled trigger on box collider if yes you must implement

    void OnTriggerEnter(Collider col)
    {

    }

Not

    public void OnCollisionEnter(Collision collision)
    {

    }