Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[Solved] OnCollisionEnter not working

Discussion in 'Scripting' started by Unity_Ghost, Dec 1, 2016.

  1. Unity_Ghost

    Unity_Ghost

    Joined:
    Dec 1, 2016
    Posts:
    6
    I want this object (script attached) to emit some particles (blood) whenever something enters it. Here's the code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Zombie : MonoBehaviour {
    5.  
    6.     void OnCollisionEnter() {
    7.         Debug.Log ("ayy");
    8.         GameObject zombie = GetComponent<GameObject> ();
    9.         GameObject.Destroy (zombie);
    10.         GameObject blood = Instantiate(Resources.Load("BloodSplatter")) as GameObject;
    11.         ParticleSystem splatter = blood.GetComponent<ParticleSystem>();
    12.         splatter.Play ();
    13.  
    14.         Destroy (splatter, 3f);
    15.     }
    16. }
    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
     
    Last edited: Dec 1, 2016
    ezed1 likes this.
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Napoleonite likes this.
  3. Unity_Ghost

    Unity_Ghost

    Joined:
    Dec 1, 2016
    Posts:
    6
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
  5. Unity_Ghost

    Unity_Ghost

    Joined:
    Dec 1, 2016
    Posts:
    6
    void OnCollisionEnter2D() {
    Didn't work
     
  6. Unity_Ghost

    Unity_Ghost

    Joined:
    Dec 1, 2016
    Posts:
    6
    Ohh nvm, OnTriggerEnter2D Worked. Ty for the help anyway :D.
     
    todddalton likes this.