- Home /
[Solved] Destroy Gameobject When A Particle Collides With It
Hi there I am just working on a simple game. When the player hits a game object it explodes and sends particles out. The simple script is below.
using UnityEngine; using System.Collections;
 public class DestroyCubesBlue : MonoBehaviour
 {
     void OnCollisionEnter (Collision col)
     {
         if(col.gameObject.name == "Safecube")
         {
             Destroy(col.gameObject);
 
             SpecialEffectsHelperBlue.Instance.Explosion (transform.position);
         }
     }
 }
I want the particles to collide with enemy cubes and destroy it :D The enemy cube just as the other cube that exploded has a box collider and a rigid body attached to it and a mesh renderer (The normal cube you can create in unity).
How I call the particles when the cube gets destroyed is using a another simple script.
 using UnityEngine;
 
 /// <summary>
 /// Creating instance of particles from code with no effort
 /// </summary>
 public class SpecialEffectsHelperBlue : MonoBehaviour
 {
     /// <summary>
     /// Singleton
     /// </summary>
     public static SpecialEffectsHelperBlue Instance;
     
     public ParticleSystem BlueExplosion;
 
     void Awake()
     {
         // Register the singleton
         if (Instance != null)
         {
             Debug.LogError("Multiple instances of SpecialEffectsHelper!");
         }
         
         Instance = this;
     }
     
     /// <summary>
     /// Create an explosion at the given location
     /// </summary>
     /// <param name="position"></param>
     public void Explosion(Vector3 position)
     {
         // 
         instantiate(BlueExplosion, position);
 
     }
     
     /// <summary>
     /// Instantiate a Particle system from prefab
     /// </summary>
     /// <param name="prefab"></param>
     /// <returns></returns>
     private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
     {
         ParticleSystem newParticleSystem = Instantiate(
             prefab,
             position,
             Quaternion.identity
             ) as ParticleSystem;
         
         // Make sure it will be destroyed
         Destroy(
             newParticleSystem.gameObject,
             newParticleSystem.startLifetime
             );
         
         return newParticleSystem;
     }
 }
All I want to do is the particles that come out destroy the enemy like a shockwave :) Thank you. I believe I have to use OnParticleCollision?
I believe you also need to check a box in the inspector to enable events as well. Also, time to pimp my own package: http://forum.unity3d.com/threads/hydra-particles-a-new-open-source-particle-sytem-for-unity3d.262813/
 using UnityEngine;
 using System.Collections;
 
 public class ParticleCollision : $$anonymous$$onoBehaviour {
 
     void OnParticleCollision (ParticleSystem Explosion){
         if (Explosion.gameObject.tag == "Enemy")
             Destroy(gameObject);
     }
 
 }
I made this. I tagged the particle system with a " Explosion " tag and the enemy with a Enemy tag. I attached it to the enemy but doesn't get destroyed. The particles are colliding with the enemy as I can see it bounce off. Anybody help?
I do get this error aswell
" This message parameter has to be of type: GameObject The message will be ignored. "
Answer by VesuvianPrime · Dec 15, 2014 at 10:02 PM
If I'm reading the error correctly, the message probably should be:
 void OnParticleCollision (GameObject other)
Thank you VesuvianPrime. $$anonymous$$ade the change you suggested and few others and seems to be working now. Please close.
Your answer
 
 
             Follow this Question
Related Questions
How to get Particles to glide on a surface 1 Answer
how to know if an object has been englobed by particles 1 Answer
can we attach a collider for each particle emitted by a emitter 1 Answer
Rigidbody - bullets becomes bullethole image 0 Answers
How to control World Particle Collider through script 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                