Physics
Beginner

OnCollisionEnter

Checked with version: 4.1

-

Difficulty: Beginner

-

Date: 27 Apr 2015

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

OnCollisionEnter

Beginner Physics

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)
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.