Search code examples
c#unity-game-engine2d-games

OnTriggerEnter2D collision.gameObject.tag Not Working & I've Setted Up Components & Written Correct Code Unity C#


I have an OnTriggerEnter2D function and I used collision.gameObject.tag and set it to the tag "Bound" without quotes, but it can't detect the tag Bound.

All of the boundaries with the tag Bound has Box Collider 2D, and the player object has Box Collider 2D. I checked "Is Trigger" without quotes for every single Box Collider 2D, and I added a Rigidbody 2D to the player object and set the gravity scale to 0. The Rigidbody 2D is Dynamic and Non-Kinematic.

Here is my code for the player (Player.cs):

 private void OnTriggerEnter2D(Collider2D collision) {
      if(collision.gameObject.tag == "Bound") {
           SceneManager.LoadScene("DeathScreen"); //I have a scene named DeathScreen
      }
 }

And not only that, but that also applies to obstacles, and their tag is "Obstacle" instead.

Any solutions?

Thanks.


Solution

  • Someone I know which is not a StackExchange member found the answer! Neither the OnTriggerEnter2D was making the problem nor SceneManager.LoadScene(...).

    I hadn't checked Simulated which was making the problem. But anyways, thanks for your answers!

    Regards,

    The Asker