MissingReferenceException when using particle system

I have two particle systems as death effect for my enemies. They are prefabs and have ‘destroy’ as stop action and i want them to instanciate every time an enemy die. This is the function called on death:

    private void ExitExplodingState() {
        aliveAnimator.SetBool("exploding", false);

        GameObject exp1 = Instantiate(deathParticles, alive.transform.position, deathParticles.transform.rotation);
        GameObject exp2 = Instantiate(deathBloodParticles, alive.transform.position, deathBloodParticles.transform.rotation);

        exp1.transform.SetParent(null);
        exp2.transform.SetParent(null);

        Destroy(gameObject);
    }

It works fine on the first enemy but then i get a MissingReferenceException on the next enemy that dies and everything gets messed up, enemys get stuck in weird states, particles appear on the wrong place, etc.

Can anyone please help me with this?

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

Thanks, I managed to solve my issue!

2 Likes

hi all!
I can only add, that such thing can be avoided if you write test first, Unity have real good documentation about how to write[ tests](http:// About Unity Test Framework | Test Framework | 1.1.33).
You can think this crazy to write test first even if there no code, well let me reassure you, by thinking about how to test stuff which is not exist you setting your programmers expectation from such point that all things happen as they suppose to happen, if you code hard to read , then you think twice and rewrite it make it smaller and test give you such freedom so that you can refactor your code whenever you wish , if all tests pass green you can deliver…