Unity 3D Collision on gameobject not working

856 Views Asked by At

I've made a code where once you collide on a gameobject, it will go to a different scene, the gameobject is a bed, I've applied a tag named Bed, I've applied a rigidbody, I've applied the code to the player where once you collide the code will activate, but for some appearent reason, it still won't work,

here is the code I've applied to the player:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

public class PlayerCollision : MonoBehaviour
{
    
  void OnCollisionEnter (Collision Collisionbad)
       {
        
          if (Collisionbad.collider.tag == "Bed")
           { 
                  Debug.Log("Roll Credits");
                  SceneManager.LoadScene("EndingScreen");
           }
        }
}
2

There are 2 best solutions below

0
Juris On BEST ANSWER

Your issue is most likely that you are checking the tag of a collider as opposed to a gameObject

if(Collisionbad.gameObject.tag == "Bed"))

Also, consider using CompareTag as it is more efficient

if(Collisionbad.gameObject.CompareTag("Bed"))
0
Bata Kos On

Add BoxColliders to both objects which collides Add this script to the object which collides with Bed In settings check layers for object 1 and 2 thath they cann collide And your script must work