Physics icon
Physics
Beginner

Detecting Collisions with OnCollisionEnter

Checked with version: 4.1

-

Difficulty: Beginner

-

Date: 24 Jul 2015

How to detect collisions between objects in code using the OnCollisionEnter function.

Detecting Collisions with OnCollisionEnter

Beginner Physics

Transcripts

DestroyCubes

Code snippet

using UnityEngine;
using System.Collections;

public class DestroyCubes : MonoBehaviour
{
    void OnCollisionEnter (Collision col)
    {
        if(col.gameObject.name == "prop_powerCube")
        {
            Destroy(col.gameObject);
        }
    }
}
#pragma strict

function OnCollisionEnter (col : Collision)
{
    if(col.gameObject.name == "prop_powerCube")
    {
        Destroy(col.gameObject);
    }
}
import UnityEngine
import System.Collections


public class DestroyCubes(MonoBehaviour):

    private def OnCollisionEnter(col as Collision):
        if col.gameObject.name == 'prop_powerCube':
            Destroy(col.gameObject)
Physics Joints
Raycasting

Related tutorials

Related documentation

Got it

The Unity Software License Agreement and Privacy Policy have been updated.

We use cookies to ensure that we give you the best experience on our website. Click here for more information.