# OnTriggerEnter2D(Collider2D)

> Sent when another object enters a trigger collider attached to this object (2D physics only).

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.0/script-reference/unityengine.md)

## OnTriggerEnter2D()

```csharp
public void OnTriggerEnter2D(Collider2D collider2D)
```

### Parameters

**** (\[Collider2D]\(/engine/6000.0/script-reference/unityengine/collider2d)):&#x20;

### Remarks

Further information about the other collider is reported in the [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) parameter passed during the call. Trigger events will be sent to disabled [MonoBehaviour](/engine/6000.0/script-reference/unityengine/monobehaviour.md)s, to allow enabling [Behaviour](/engine/6000.0/script-reference/unityengine/behaviour.md)s in response to collisions. Additional Resources: The [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) class and the OnTriggerExit2D and OnTriggerStay2D messages. An OnTriggerEnter2D example is shown. This example has two empty [GameObject](/engine/6000.0/script-reference/unityengine/gameobject.md)s, called GameObject1 and GameObject2. These both have script files which makes the example work. The first script, Example1, creates a [Sprite](/engine/6000.0/script-reference/unityengine/sprite.md) and adds a [BoxCollider2D](/engine/6000.0/script-reference/unityengine/boxcollider2d.md) and a [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md). This object falls under gravity and collides with Example2. Example2 has no visibility. (The rectangle it creates is visible in the Scene window.) Both [GameObject](/engine/6000.0/script-reference/unityengine/gameobject.md)s report the collision. The script below is Example1.

### Examples

```csharp

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Create GameObject1 that falls under gravity.  It will pass through
// Example2 and cause a collision.  GameObject1 is moved back to
// the start position and it will again start to fall under gravity.

public class Example1 : MonoBehaviour
{
    void Awake()
    {
        SpriteRenderer sr;
        sr = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer;
        sr.color = new Color(0.9f, 0.9f, 0.1f, 1.0f);

        BoxCollider2D bc;
        bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
        bc.size = new Vector2(1.0f, 1.0f);

        Rigidbody2D rb;
        rb = gameObject.AddComponent<Rigidbody2D>() as Rigidbody2D;
        rb.gravityScale = 1.0f;

        // A square in the Resources folder is used.
        gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("square");

        // GameObject1 starts 3 units in the Up direction.
        gameObject.transform.position = new Vector3(0.0f, 3.0f, 0.0f);
        gameObject.transform.localScale = new Vector3(0.5f, 0.5f, 1.0f);
    }

    private float timer = 0.0f;
    private bool restart = false;

    void FixedUpdate()
    {
        if (restart == true)
        {
            timer = timer + Time.deltaTime;
            if (timer > 0.25f)
            {
                gameObject.transform.position = new Vector3(0.0f, 3.0f, 0.0f);
                gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(0.0f, 0.0f);
                restart = false;
            }
        }
    }

    // called when this GameObject collides with GameObject2.
    void OnTriggerEnter2D(Collider2D col)
    {
        Debug.Log("GameObject1 collided with " + col.name);
        restart = true;
        timer = 0.0f;
    }
}
```
