Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


collider

The Collider2D detected by the physics query.
Read time 1 minuteLast updated 4 days ago

Definition

  • Type: Property
  • Namespace: UnityEngine
  • Assembly: UnityEngine.Physics2DModule
public Collider2D collider { get; }

Remarks

When the RaycastHit2D result is returned from a physics query, the
collider
refers to the specific Collider2D that was detected.
NOTE: This field will be NULL if nothing was detected however when checking if the result is valid, you should use the less verbose method shown in the code example below.
Additional Resources: RaycastHit2D.rigidbody.

Examples

using UnityEngine;public class ExampleClass : MonoBehaviour{ public Vector2 direction; void Update() { // Cast a ray in the specified direction. RaycastHit2D hit = Physics2D.Raycast(transform.position, direction); // If something was hit, delete the specific Collider we hit. if (hit) Destroy(hit.collider); }}