0

that's my very first question in here (not a first visit searching solution, though ;D).

I've got an issue with ParticleSystem in Unity:

    if (!dead) {
        dieBang.enableEmission = false;
        Vector2 newVelocity = rigidbody2D.velocity;
        newVelocity.x = forwardMovementSpeed;
        rigidbody2D.velocity = newVelocity;
    } else
        dieBang.enableEmission = true;
    UpdateGroundedStatus ();

The problem is dieBang.enableEmmision gets true only when dead state is caused by on object(laser) I manually "put" in the game. When character hits laser generated of prefab, it gets dead, stop moving etc. , but there is no dieBang :D Hope you can help me

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Wojtek K
  • 3
  • 2
  • 1
    Side note: please make sure tags added to your posts are reasonable - i.e. "unity" is for DI injection framework, "unity3d" is for game engine framework. I've re-tagged this question as it is unlikely related to DI. – Alexei Levenkov Feb 05 '15 at 15:43
  • Where do you instantiate the particles gameobject? – laminatefish Feb 05 '15 at 16:17
  • @LokiSinclair , I declared it : `public ParticleSystem dieBang;` – Wojtek K Feb 05 '15 at 16:33
  • Declared and dragged in via the editor, yes? In which case, you'll still need to instantiate(dieBang, someGameObject.transform.position, Quaternion.identity) - When you want to see it. Otherwise, it'll play once and then be destroyed. Any NullReferenceExceptions will be displayed in the Console. – laminatefish Feb 05 '15 at 16:54

1 Answers1

0

It's hard to say with the limited info here but I have a hunch that your particle system is being destroyed with the character so you will never see the particle effect.

Either destroy the character after the effect is complete or instantiate the particle system without a parent so it isn't destroyed by accident.

EDIT: Also worth noting, if you're simply disabling the character when it's hit instead of destroying the game object, you'll have the same effect because the parent is disabled, thus disabling the children(ie. the particle system).

  • I don't destroy the character, when it dies, actually. It just stops and falls to the ground. – Wojtek K Feb 05 '15 at 16:39
  • Hmm... Try this: dieBang.enableEmission = true; dieBang.Play(); – Robert Rathgeber Feb 05 '15 at 17:08
  • It helped, `dieBang` started emmision after hiting prefab-laser. But, I set it not to loop, to make just one "bang". Now it's looped. I guess it's because `dieBang.Play()` is inside `Fixed Update`, so the `dieBang.Play()` method is called continously? Is there a way to play it just once? I'm really a newbie (3 days) with Unity, I'm learning with tutorial , and tried to apply some modifications of my own ;p – Wojtek K Feb 05 '15 at 21:04
  • There are a number of ways to structure your code. Based on what I've seen, I would move the `dieBang.enableEmission = true` and `dieBang.Play()` in the same function that changes `dead` to `true` – Robert Rathgeber Feb 05 '15 at 23:22