Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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"

avatar image Magso UnityinTraining · Jun 12, 2019 at 09:26 PM 0
Share

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"

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.

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

Why are my Game Objects being destroyed? 0 Answers

Get children of another object in hierarchy. 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


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