- Home /
Is it possible to destroy a gameobject from a child gameobject's script?
I am trying to destroy the parent Gameobject from the child. The parent is "Opossum" that has a patrol and attack script attached to it. The Rigidbody2D is "OposBody" The child only has the OpossHealth script attached to it. Unless... should I put the trigger inside of the if statement instead of this way?
public int enemyHealth, currentHealth, playerdmg;
GameObject PTarget, OpossBody, Opossum;
public void OnTriggerEnter2D(Collider2D OpossBody)
{
if (OpossBody.gameObject.name == "Opossum")
{
if (currentHealth <= 0)
{
Destroy(Opossum.gameObject);
}
}
}
I'm not sure if you know this but OnTriggerEnter2D(Collider2D OpossBody)
means that the intersecting object will be assigned as OpossBody
within the trigger function.
yes, I do, but it still doesnt destroy the game object "Opossum"
Ok, I thought you was trying to reference the parent in OnTriggerEnter from the child but I got the wrong idea.
It might not be working because you have 2 OpossBody variables, I'll be surprised if there's no error or warning about this in the console. Try something like this.
public void OnTriggerEnter2D(Collider2D OpossBodyCollider)
{
if (OpossBodyCollider.gameObject.name == "Opossum")
{
or
public void OnTriggerEnter2D(Collider2D OpossBodyCollider)
{
if (OpossBodyCollider.gameObject == Opossum)
{
if the variable Opossum is the object named "Opossum"
Answer by Doctor_ED · Jun 17, 2019 at 07:45 AM
Assuming that I understood what is it all about... You are trying to destroy GameObject that is stored in Opossum
variable, but there's no check as to what that variable holds. (That variable doesn't have to be your "Opossum") to fix :
public int enemyHealth, currentHealth, playerdmg;
GameObject PTarget, OpossBody, Opossum;
public void OnTriggerEnter2D(Collider2D OpossBody)
{
if (OpossBody.gameObject.name == "Opossum")
{
if (currentHealth <= 0)
{
Destroy(OpossBody.gameObject);
}
}
}
That way you will destroy gameObject that entered trigger (if it's name is "Opossum" and currentHealth
<=0)
Disclaimer:
Comparing names is not the best way of checking if given GameObject is the one you're looking for (as comparing strings is performance expensive, and if you were to just change the name of GameObject whole script falls apart), instead it's better to use something like
if (OpossBody.GetComponent<ScriptNameThatIsExclusiveForThatGameObject>() != null)
(In other words, if given GameObject has that script f.eg. "PlayerInputHandler" or like "PlayerSomething" then you are sure that GameObject is a player).
And... Question itself is kinda chaotic, after reading it I instantly got so many questions, like :
That script you've pasted, what is it's name, and to which gameObject is it attached to?
You mentioned "OposBody", but in provided screenshot there's no such a gameObject.
On which of your gameObject's sits the collider (the one that is NOT trigger, the one that you want to detect), on "Hitbox", "detection" or on the parent "Opossum"?