Problem instantiating a dead replacement gameObject for killed enemy
Context
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.
Content
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.
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.
A Projectile script applied on PotatoAmmo that instantiates an explosion (from Particle System) OnCollisionEnter at the PotatoAmmo contact point with other colliders.
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.
Problem
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.
P.S.
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.
are there any errors in the console? are you sure deadReplacement gameobject exists when the DestroyBun method is called?
Hi xxmariofer. Console is clean. The deadReplacement gameObject is declared at the top of my ReceivedHit script as follow:
public class ReceivedHit : $$anonymous$$onoBehaviour
{
public GameObject game$$anonymous$$anager; // the master repository for game info
public GameObject deadReplacement; // ToastedZombie replacement when Zombie Bunnies are hit
public GameObject smokeplume; // smoke particle system
void Start()
{
game$$anonymous$$anager = GameObject.Find("Game $$anonymous$$anager"); // identify and assign the Game $$anonymous$$anager Objet to game$$anonymous$$anager variable
}
but are you sure you have drag and dropped and never deleted an instante of deadReplacement?
Answer by LagSik · Dec 15, 2020 at 02:10 AM
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.
change
deadReplacement.transform.gameObject;
to simply
deadReplacement;
it is redundant
xxmariofer, you are my saviour ! Thanks a lot for your time and patience.
Your answer
Follow this Question
Related Questions
Scope Animation doesn't work?! 0 Answers
Problem with destroying a gameobject 2 Answers
Multiple Instantiations not working 0 Answers
How to instantiate a game object every time when space key is pressed 2 Answers