[Solved] OnCollisionEnter not working

I want this object (script attached) to emit some particles (blood) whenever something enters it. Here’s the code

using UnityEngine;
using System.Collections;

public class Zombie : MonoBehaviour {

    void OnCollisionEnter() {
        Debug.Log ("ayy");
        GameObject zombie = GetComponent<GameObject> ();
        GameObject.Destroy (zombie);
        GameObject blood = Instantiate(Resources.Load("BloodSplatter")) as GameObject;
        ParticleSystem splatter = blood.GetComponent<ParticleSystem>();
        splatter.Play ();

        Destroy (splatter, 3f);
    }
}

The code doesn’t even execute the Debug.Log statement.

The car that touches the zombie has a rigidbody (2D). I tried adding the rigidbody (2D) to the zombie as well, didnt really work.

Solution: OnTriggerEnter2D Since I’m working on a 2D Game

1 Like

Does any of the two collidable objects have a non kinematic rigidbody? Otherwise the OnCollission events don’t work: Unity - Scripting API: Collider.OnCollisionEnter(Collision)

1 Like

Yep, The car that touches the zombie has a rigidbody (2D). I tried adding the rigidbody (2D) to the zombie as well, didnt really work.

2D? Then use OnCollissionEnter2D :slight_smile:

3 Likes

void OnCollisionEnter2D() {
Didn’t work

Ohh nvm, OnTriggerEnter2D Worked. Ty for the help anyway :smile:.

1 Like