Huh? I told you to explode over THERE

So no matter how awesome a tool is, it always has some bugs in it. Unity3D doesn’t escape this unfortunate reality. So far I haven’t come across anything that was so bad it made the tool unusable, just delayed development for a little bit while I searched for a solution. One of these bugs reared it’s ugly head during development of Shape Sprout while trying to create a small explosion for when Kari’s “shape bullets” hit Jumble’s target on his balloon. So, I created a particle system, set the parameters to make it look like a little explosion, turned off looping and then created a prefab out of it so I could instantiate it easily when needed in the game. So far so good, the collider was triggering the callback function on the target and I setup up the transform location of the newly created explosion at the point that the “shape bullet” hit the target. Something like this…

GameObject bulletHit = (GameObject) GameObject.Instantiate(bulletHitPrefab);
bulletHit.transform.parent = other.transform.parent;
bulletHit.transform.localPosition = other.transform.localPosition;
bulletHit.particleSystem.Play();
Diagnostic.Logit(other.transform.position);
Diagnostic.Logit(bulletHit.transform.position);

Every once in a while though, the explosion would happen at some random spot on the screen instead of where the “shape bullet” hit the target.

Particle System Positioning Oddity
Um…how’d the explosion get over there?

What in the world? I must have been doing SOMETHING wrong, but the same exact code was running each and every time. Timing maybe? Lets move some code around where things are created and destroyed.  Nothing. Still had the same problems.  Even made a log of the location of both where the explosion should happen and where the explosion supposedly was and they were exactly the same.  So why wasn’t it SHOWING it in the right spot? I started looking around a bit online and found something about the particle system needing to be disabled first before positioning it, then re-enable it.  AND make sure “Play on Awake” wasn’t enabled. Okay, so lets add a little bit into the code here…

GameObject bulletHit = (GameObject) GameObject.Instantiate(bulletHitPrefab);
bulletHit.SetActive(false);
bulletHit.transform.parent = other.transform.parent;
bulletHit.transform.localPosition = other.transform.localPosition;
bulletHit.SetActive(true);
bulletHit.particleSystem.Play();

And lets see what happens…

Particle System Positioning Fixed
That’s more like it

Maybe this is expected “behaviour”. (See what I did there?) Maybe I’m just doing it wrong? All in all I would not trade Unity3D for anything right now. Maybe this will help someone else dealing with this odd particle system positioning bug/issue. As of this writing, I am using Unity version 4.5.2f1.