Physics
Beginner

OnCollisionEnter

Checked with version: 4.1

-

Difficulty: Beginner

-

Date: 13 Mar 2014

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