• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by UnityinTraining · Jun 11, 2019 at 10:09 PM · triggerdestroy2d-platformergameobjectschild object

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? alt text

     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);
             }
         }
     }


child-parent.jpg (11.3 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Magso · Jun 11, 2019 at 11:06 PM 0
Share

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.

avatar image UnityinTraining Magso · Jun 12, 2019 at 08:05 PM 0
Share

yes, I do, but it still doesnt destroy the game object "Opossum"

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Doctor_ED · Jun 17, 2019 at 08:04 AM 0
Share

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"?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detect if object is child, from extern script 1 Answer

Do something only "for" Gameobjects in trigger 1 Answer

Target touching enemy collision 1 Answer

How to Disable/Enable another Script with Triggers? 1 Answer

Explosion triggers another explosion 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges