- Home /
 
Activating Particle Effect on Collision:
So I have a Particle Effect prefab attached to an object - object contains a Rigid Body and Box Collider. This Particle Effect should play once when it comes in contact with another object with a Collider. I've set the Collider on the object as: "Is Trigger".
This is the script I've been working on. I'm obviously missing components but hopefully moving in the right direction:
using UnityEngine;
using System.Collections;
     public class ParticleStarter : MonoBehaviour{
 void OnCollisionEnter (Collision col)
 {
     if(col.gameObject.other (ParticleEmitter)
     
        ParticleEmitter.enabled = (true);
        }
 
               }
Any help would be appreciated.
Answer by DanSuperGP · Jan 15, 2015 at 11:33 PM
You need to have a reference to your specific particle system. If the script is on the same game object, the easy way to do it is this. You can also make _psystem public and drag the particle system reference onto it.
 [RequireComponent(typeOf(ParticleSystem))]
         
 public class ParticleStarter : MonoBehaviour{
             
      private ParticleSystem _psystem;
         
       void Awake() {
           _psystem = GetComponent<ParticleSystem>()
        }
         
        void OnTriggerEnter (Collider col)  {
 
   
                  _psystem.Play();
       
         }
         
  }
 
              Right now, the particle effect shows a default setup that's already in the Active state once the Object appears.
I changed the "private ParticleSystem_psystem" to "public ParticleSystem_psystem;"
Afterwards, I assigned my particle system within the Inspector when the option became visible.
Right now, my specified particle effect does not appear. Also, the default particle effect turns on right away, regardless whether there was a collision detected or not.
Did I miss something?
Well... if you're going to make it public. you should delete the RequireComponent and GetComponent lines.
Also, you should turn off playonawake on the particle system you want to trigger.
If the script isn't on the same object as the particle system you are trying to activate, it's probably creating a new default particle system... and then overriding your publicly assigned particle system.
I think there is a issue happening when I have the collision effect and spawn effect happening at the same time. At one point, it was working, then it suddenly stopped after a few collisions.
Your answer