Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 BarnsChambers · Jan 05, 2014 at 09:06 AM · collisionparentchild

OnCollisionEnter(Collision Other) setActive affecting parent

Hey guys and thanks for reading.

I have a whole bunch of objects that are linked up to a parent. Whenever one of my bullets hits any of the children objects, all the the objects are being setActive(false); I only want the object which was collided with to be set active. There are 10 or so objects, so having them not under a parent object isn't really an option. This event does not occur if the objects do not have a parent.

Here is the code (attached to the bullet which hits the objects I want to be setActive(false);

  void OnCollisionEnter(Collision other){

     
          if(other.collider.transform.tag == "Cube"){
         
         
              Debug.Log("DESTROYING CUBE");
                  other.gameObject.SetActive(false);
         
              Destroy(this.gameObject);
         
             }
     
      }

}

Comment
Add comment · Show 1
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 Benproductions1 · Jan 05, 2014 at 09:06 AM 0
Share

Please format your code, if you don't know how, watch the tutorial video on the right!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sath · Jan 05, 2014 at 10:02 AM

if bullet hit a child it will destroy only this child but if the bullet hit the parent first then all cubes will be destroyed..

To avoid this set parent's tag with different name ("parentCube"),child tag("cube") and rewrite like

 //set parent's health script
 //Health damage;
 
 void OnCollisionEnter(Collision other){
 
     if(other.collider.transform.tag == "cube"){
     
         Debug.Log("child hitted");
         
         other.transform.parent=null; 
         other.gameObject.SetActive(false);
         
         Destroy(gameObject);//destroy bullet
     
     }
     
     if(other.collider.transform.tag == "parentCube"){
     
         Debug.Log("Parent hitted");
         
         //here you can set how bullet is making damage to parent
         //If you have a script on parent with health  you can say :
         //damage = other.transform.GetComponent<Health>();//where Health is the name of script is attached to parent
         //damage.health-=10;//health is public int health=100; inside Health script
         //But if you decide to Destroy parent without destroying the children
         //other.transform.DetachChildren();
         //Destroy(other.gameObject);
         
         Destroy(gameObject);//destroy bullet
     
     }
 }

Everything is depending from your game logic or what you would like to happen to your parentObject..

Comment
Add comment · Show 5 · 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 BarnsChambers · Jan 06, 2014 at 04:55 AM 0
Share

That didn't work for some reason :/

avatar image BarnsChambers · Jan 06, 2014 at 04:58 AM 0
Share

Still destroying the whole parent and children

avatar image sath · Jan 06, 2014 at 09:48 AM 0
Share

edit up my answer .. hope this is more helpful

avatar image BarnsChambers · Jan 09, 2014 at 01:51 PM 0
Share

Thanks for your input. I understand how that code is meant to work, however it doesn't for my code.

If I remove the parent's rigidbody however, the code works fine. ALTHOUGH I am unable to do this as I need the parent to have a rigidbody.

SO FRUSTRATING :C

avatar image BarnsChambers · Jan 09, 2014 at 01:53 PM 0
Share

Also the parent object is untagged, so that isn't the issue

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

19 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

Related Questions

Make a simple tree 1 Answer

Attaching objects on collision 1 Answer

How do i kill an "enemy with character controller on + few child objects on" 1 Answer

Getting a Trigger's parent 1 Answer

How to detect collision on child objects from the parent?? 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