unity-game-engineparticle-systemsteamvr

playing particle system in Unity


I am using Unity3D to develop for the HTV Vive using SteamVR. I have downloaded an asset from the asset store with explosion effect created using a particle system. I want to play the particle animation when an object is destroyed. Here is the code I am, unsuccessfully, using.

private void OnDestroy() {
    explosion.GetComponent<ParticleSystem>().Play();
}

Explosion is a public variable of type GameObject set from the inspector. I drop the particle system object there.

Why is it not working? anyone a good recommendation on a short tutorial to learn to use (not to create) particle effects?

Thanks


view of the hierarchy

view of the hierarchy

I have tried this with the PS as a child of the target and as an independent object.

view of the inspector (Target)

view of the inspector (particle system)

edit: for some reason, the particle effect is destroyed right after the scene starts.


Solution

  • Try making the explosion effect into a prefab and instantiate it when destroyed.

    GameObject explosion; // Prefab asset
    private void OnDestroy() {
        Instantiate(explosion, transform.position, Quaternion.identity);
    }
    

    Also, don't forget the stop action to Destroy. enter image description here