- Home /
My OnTriggerEnter2D(Collider2D other) not working
Hi there, I have a script which I attached it to my obstacles, and when the player collides with the obstacles, a debug log should print out a message "Player Respawn". However, my OnTriggerEnter2D in the script is not working and I dont know why. Can someone help me please?
public class KillPlayer : MonoBehaviour {
public LevelManger levelManager;
// Use this for initialization
void Start ()
{
levelManager = FindObjectOfType<LevelManger>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
levelManager.RespawnPlayer();
}
}
}
And below is the script for LevelManger:
public class LevelManger : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void RespawnPlayer()
{
Debug.Log("Player Respawn");
}
}
And this is my Player:
And this is my obstacle which I attached KillPlayer script to:
Answer by FlaSh-G · Aug 07, 2017 at 10:37 PM
Your Player is tagged with "Untagged", not "Player".
Also, your levelManager variable has no value set in the inspector, so you'll get a NullReferenceException once you fixed the tag; unless you drag your LevelManager GameObject into the inspector.
oh.. omg i just realised it too...................................... thanks alot !!!!!
Your answer
Follow this Question
Related Questions
Collision on specific Frames 1 Answer
How to only delete one of two collided objects? 1 Answer
Multiple Cars not working 1 Answer
[c#]collision script not working 1 Answer
Box Collider 2d Not Colliding 0 Answers