0
votes

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.

1
Can we see the GameObject heirarchy with both the object containing this script and the object that the ParticleSystem is attached to? Also, a view of the inspector with how explosion is set? - Foggzie
I have updated the previous post with the images - Anzu67
Am I correct to say that target is the object in which you call the play function when it is destroyed? if so, since the particle system is a child of that GameObject, it will get destroyed when the target is destroyed. What happens if the particle system isn't a child? - Ryolu
Did you try to disable Play On Awake * ? - derHugo
@Ryolu except it is the OP or author of a post (answer) ;) those users will allways be notified - derHugo

1 Answers

0
votes

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