I am attempting to make a script in Unity 2D.

Basically, when the player touches a door collider, it would log "Is on door".

using UnityEngine;public class ChangeScene: MonoBehaviour {void OnCollisionEnter2D(Collision2D collision) {if (collision.gameObject.name == "Door") {Debug.Log("Is On Door");}}}

But when I touch the collider, nothing happens.

How can I check if the collision game object name is Door?

*I don't want to compare with any tag or layer, I want to compare with the name of the GameObject itself.

1

Best Answer


This script should be on the GameObject Door to check for other GameObjects it collides with:

public class ChangeScene: MonoBehaviour {void OnCollisionEnter2D(Collision2D collision) {if (collision.gameObject.name == "door") {Debug.Log("Is on Door");}}}