Particle Effects Not Occuring

If you click blocks, they are destroyed with a 1 second delay in between clicks. I am trying to add a particle effect when they are destroyed, but without any luck. I commented out the two lines of code I tried. Any suggestions what I’m doing wrong?

using UnityEngine;
using System.Collections;

public class destroyBlock : MonoBehaviour {

    static float lastClickTime = -999;
    // public GameObject destroyParticles;

    void OnMouseDown () {
        if (Time.time - lastClickTime < 1) return;
        lastClickTime = Time.time;
        Destroy (gameObject);
        // Instantiate(destroyParticles, transform.position, Quaternion.identity);
    }
}

// Instantiate(destroyParticles, transform.position, Quaternion.identity);[/QUOTE]

Are you sure you didn’t just forgot to remove the // ?

Haha, yes. I just did that to show the lines of code that pertain to the particle effect.

Please try something like this:

using UnityEngine;
using System.Collections;

[RequireComponent( typeof(BoxCollider) )]

public class DestroyGameObjectScript : MonoBehaviour
{
     public GameObject particlePrefab;
     public float time = 0f;    // if 0 == instant

     private Coroutine m_coroutine = null;

     void OnMouseDown()
     {
          if (m_coroutine != null)
               return;

          m_coroutine = StartCoroutine( DestroySequence() );
     }

     IEnumerator DestroySequence()
     {
          if ( time > 0)
               yield return new WaitForSeconds( time );

          Destroy( gameObject );

          if ( particlePrefab == null)
               yield break;

          ParticleSystem system = particlePrefab.GetComponent<ParticleSystem>();
          if ( system == null)
          {
               Debug.LogError("DestroyGameObjectScript Error: Provided particlePrefab does not contain ParticleSystem!");
               yield break;
          }

          GameObject instance = Instantiate( particlePrefab, transform.position, transform.rotation) as GameObject;
          system = instance.GetComponent<ParticleSystem>();
          system.Play();

          float particleDuration = system.startLifetime + system.duration;
          Destroy(instance, particleDuration);
     }
}

Attach this script to your object, provide a particle prefab within inspector. Using the coroutine we can trigger this code once, and if the time is set to 0, gameObject will be destroyed immediately. In case you placed a prefab without particle system, debug error will notify you in case you made a mistake.

1 Like

Thank you for your response!

This made the particle system work, but the destroy/particle effect is delayed after the click by the time variable(1 second for me). Maybe I worded it wrong.

If you look at the original script, it limits you to one click per second, not delay the actions by one second after the click.

Does that make more sense?

Nevermind. I placed some of my old script in the void MouseDown(). Thank you so much!