Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • Overview
  • Trigger Events When Touching / Stop Touching
  • Trigger Events When Entering, Staying, and Exiting a "Trigger"
  • Updating the Collider Component within the Script
  • Related Links

Was this helpful?

  1. Physics
  2. Colliders and Triggers

Accessing Colliders Through Scripts

PreviousCollider Component OverviewNextCommon Issues: Colliders and Triggers

Last updated 4 years ago

Was this helpful?

Overview

Colliders can be .

Example of storing a Collider component in a variable:

public Collider thingCollider;

In the Inspector, the object with the script attached will have the component for the script and this property:

You can click and drag an object with a collider into the field or click the circle target icon on the right to select from a list.

If you want to have collider of the object the script is attached to be stored, use this script in the Start() function:

thingCollider = gameObject.GetComponent<Collider>();

Although the collider components are named "Box Collider" or "Capsule Collider" etc. in the editor, the component can be referenced as "Collider" in the script.

Trigger Events When Touching / Stop Touching

Here is a script that runs code when objects touch or stop touching (put this after the ending bracket of Update() but before the ending bracket of the class):

public void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.tag == "Colliding Object")
    {
        // When something with the tag "Colliding Object" 
        // hits this object
        // Run this code on the first frame this is true
    }
}

public void OnCollisionStay(Collision collision)
{
    if (collision.gameObject.tag == "Colliding Object")
    {
        // When something with the tag "Colliding Object" 
        // continues to touch this object
        // Run this code each frame
    }
}

public void OnCollisionExit(Collision collision)
{
    if (collision.gameObject.tag == "Colliding Object")
    {
        // When something with the tag "Colliding Object" 
        // no longer touches this object
        // Run this code the first frame this is true
    }
}

In this example, the colliding object's tag is checked so the code only runs when that specific object(s) collide with the object this script is attached to. The if statements can be updated to look at the name, layer, etc. This if statement keeps the code from running when other objects collide or are inside the object.

Trigger Events When Entering, Staying, and Exiting a "Trigger"

Here is a script that runs code when objects enter, stay, or exit a collider that has the Is Trigger checkbox checked (put this after the ending bracket of Update() but before the ending bracket of the class):

public void OnTriggerEnter(Collider other)
{
    if (other.tag == "Entering Object")
    {
        // When something with the tag "Entering Object" 
        // enters this object's collider
        // Run this code on the first frame this is true
    }
}

public void OnTriggerEnter(Collider other)
{
    if (other.tag == "Entering Object")
    {
        // When something with the tag "Entering Object" 
        // continues to stay inside this object's collider
        // Run this code each frame
    }
}

public void OnTriggerEnter(Collider other)
{
    if (other.tag == "Entering Object")
    {
        // When something with the tag "Entering Object" 
        // exits this object's collider
        // Run this code the first frame this is true
    }
}

In this example, the entering object's tag is checked so the code only runs when that specific object(s) enter, stay, and exit the collider of the object this script is attached to. The if statements can be updated to look at the name, layer, etc. This if statement keeps the code from running when other objects are inside the object.

Updating the Collider Component within the Script

You can update whether or not the collider is a trigger with this code:

gameObject.GetComponent<Collider>().isTrigger = true;

Related Links

Accessing Components Through Scripts
accessed like any other component