Problem instantiating a dead replacement gameObject for killed enemy


Hello Unity Community. I’m new to programming and it’s my first post here. I’ve decided to star my journey with the book Unity for Absolute Beginners (Blackman, 2014). I think I’m blocked in my progression due to some minor changes in scripting references between 2014 and 2020. Here’s my situation.


I have a first-person player-character shooting explosive PotatoAmmo at ZombieBunny. When a potato hit a Bunny, the critter disappears. This process is going well until I try to replace the shot Bunny with a dead replacement with an animation. There are 3 main scripts involved here.

  1. A PotatoLaucher script applied on an empty gameObject pointing forward (z axis) from gun point. It instantiates a clone of the PotatoAmmo at the location & orientation of the script’s parent and adds forward velocity to the rigidbody of PotatoAmmo.

  2. A Projectile script applied on PotatoAmmo that instantiates an explosion (from Particle System) OnCollisionEnter at the PotatoAmmo contact point with other colliders.

  3. A ReceivedHit script applied on ZombieBunny whose ifStatement is to look for collision with PotatoAmmo and “if true” call a function named DestroyBun() that destroy ZombieBunny.


Every think works perfectly until I want to replace the shot ZombieBunny with another prefab named ToastedZombie (which is a child of a parent Toasted Zombie Parent) with a disappearing animation named “Jump Shrink”.

Here is the problematic ifStatement added in the DestroyBun() function:

    if (deadReplacement)
        // get the dead replacement object's parent
        GameObject deadParent = deadReplacement.transform.parent.gameObject;

        // instantiate the dead replacement's parent at this object's transform
        GameObject dead = (GameObject)Instantiate(deadParent, transform.position, transform.rotation);

        // trigger its default animation
        deadReplacement.GetComponent<Animator>().Play("Jump Shrink");

        // destroy the dead replacement's parent after a second
        Destroy(dead, 1.0f);

Here is the core problem: When this block of code is added:

  • ZombieBunny stop disappearing on contact with PotatoAmmo.

  • The GameObject variable deadReplacement (assign with the ToastedZombie) isn’t instantiated in the scene on contact.

  • The “Jump Shrink” animation isn’t played.


I know it may sound like a noob question, but I can’t manage my way around it and I can’t progress further in the book. If someone could gently give me some advices (with detail ideally) or share some documentation, I would be really grateful.


The screenshot shows the prefab hierarchy where my ToastedZombie is parented with the empty gameObject Toasted Zombie Parent. From that, I understand that when I’m drag-dropping my ToastedZombie from the Project view into the deadReplacement in script component, the parent isn’t include. That’s a great lesson.

To workaround that, I replaced this line:

GameObject deadParent = deadReplacement.transform.parent.gameObject;

with this one:

GameObject deadParent = deadReplacement.transform.gameObject;

Then, I drag-dropped the Toasted Zombie Parent onto my Dead Replacement on the ReceivedHit script component of my ZombieBunny.

I don’t know if it’s a good coding practice, but it worked. Thanks a lot for your insight. Much appreciated.