0

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.

derHugo
  • 83,094
  • 9
  • 75
  • 115
Anzu67
  • 23
  • 6
  • 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 Oct 22 '18 at 20:46
  • I have updated the previous post with the images – Anzu67 Oct 22 '18 at 21:01
  • 1
    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 Oct 23 '18 at 01:01
  • The same thing happens when the particle object is not the child. It plays as soon as the scene starts and is destroyed – Anzu67 Oct 23 '18 at 02:40
  • 1
    Did you try to disable `Play On Awake *` ? – derHugo Oct 23 '18 at 05:43
  • Continuing from what @derHugo mentioned, since the particle effect plays immediately on awake, it will play for the indicated 0.6s before destroying itself as soon as the scene starts. One thing to note, if you don't @ the user you are replying to, the user will not be notified of your reply to their comment – Ryolu Oct 23 '18 at 06:25
  • 1
    @Ryolu except it is the OP or author of a post (answer) ;) those users will allways be notified – derHugo Oct 23 '18 at 06:27
  • @Ryolu: Yes, I have tried to uncheck the play on awake. This does prevent the particle effect to be destroyed, but it is still not playing when I use the .Play() method – Anzu67 Oct 24 '18 at 19:51
  • @derHugo: see above – Anzu67 Oct 24 '18 at 19:51
  • Does it even call `Play()`? – Ryolu Oct 25 '18 at 00:47
  • Please also show us the `Emission` settings. You either need to have `Rate over time`, `Rate over distance` different to `0` or a `burst` configured. Actually the system should only be destroyed if you configure that as `Stop Action` .. see also [this great tutorial](https://www.raywenderlich.com/138-introduction-to-unity-particle-systems) – derHugo Oct 25 '18 at 03:40
  • Did you also try [this](https://stackoverflow.com/q/34856642/7111561) ? – derHugo Oct 25 '18 at 05:08

1 Answers1

0

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

MysticalUser
  • 404
  • 5
  • 13