2D Collision & Trigger

This script is attached to the player:

using UnityEngine;
using System.Collections;

public class RestartLevel : MonoBehaviour {

    void OnTriggerEnter2D(Collider2D OtherObject) {
        if(OtherObject.gameObject.CompareTag("Enemy")) {
            Debug.Log ("Collision");
            Destroy (OtherObject);
        }
    }

}

There is also a “Box Collider 2D” attached to the player and it is also used as a trigger.
To every enemy in the game I also attached a Box Collider 2D, whenever the player touches an enemy the player is suppose to be destroyed but nothing is happening, it goes through them.

Tried simply, othergameobject.tag == “Enemy”?

I believe the 2d physics is the same as the 3d physics in that something in the collision is required to have a RigidBody2D attached to it so the 2d physics engine knows about it.

oh and it needs to be

Destroy(OtherObject.gameObject);

if you want to destroy the entire thing and not just the collider on the otherobject. mutters about naming variables :stuck_out_tongue:

1 Like

I added a rigidbody 2d and it worked, thank you :slight_smile: