• 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 /
This question was closed Mar 19, 2021 at 10:14 PM by Asaioki.
avatar image
0
Question by Asaioki · Mar 22, 2021 at 11:25 AM · parentchildren

Why does this function still reference a destroyed child?

[Initial question deleted, it was not the root of the problem, see edit.]


Edit: Quick explaination, this script is one that assigns a cursor object to the mouse. On selection of a new cursor object this script is called, where the old cursorobject is deleted and a new one added: it would seem that the problem actually lies with the previous "mousecursor" object, which is getting destroyed, is still being referenced?:

 public void SetObjToPlace(GameObject obj)
                  {
                      objToPlace = obj;
                      cursorChildren = GetComponentsInChildren<Transform>();
              
                      // Clear collisionScripts
                      collisionScripts.Clear();
              
                      // Delete old object cursor
                      if (cursorChildren != null)
                      {
                          foreach (Transform child in transform)
                          {
                              Destroy(child.gameObject);
    //  ^ THIS OBJECT STILL GETS REFERENCED IN AddRigidBodyAndTriggerToAllChildren
                          }
                          cursorChildren = GetComponentsInChildren<Transform>();
                      }
              
                      // Create new object cursor
                      GameObject newCursor = Instantiate(obj);
                      newCursor.transform.parent = transform;
              
                      AddRigidBodyAndTriggerToAllChildren(transform);
             // ^ This ^ function throws the error "Can't add component 'Rigidbody' to NameOfPreviouslyDestroyedObject because such a component is already added to the game object (well no shit, but it shouldn't check that destroyed child)
              
                      transform.rotation = Quaternion.identity;
                      newCursor.transform.position = transform.position;
                      SetChildShader();
                  }
         
             private void AddRigidBodyAndTriggerToAllChildren(Transform parent)
                 {
                     foreach (Transform child in parent)
                     {
                         BoxCollider[] childColliders = child.gameObject.GetComponents<BoxCollider>();
                         if (childColliders.Length > 0)
                         {
                             Rigidbody childRB = child.gameObject.AddComponent<Rigidbody>();
         // ^ This is the line throwing the error ^
                             childRB.useGravity = false;
         // ^ Causing a nullref for childRB here 
                             childRB.isKinematic = true;
             
                             PlacementCollision collisionScript = child.gameObject.AddComponent<PlacementCollision>();
                             collisionScripts.Add(collisionScript);
             
                             foreach (BoxCollider collider in childColliders)
                             {
                                 collider.isTrigger = true;
                             }
                         }
             
                         AddRigidBodyAndTriggerToAllChildren(child);
                     }
                 }

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 Hellium · Mar 19, 2021 at 08:24 PM 0
Share

Are you sure you simply don't call the function twice?

Add a Debug.Log outside the loop

1 Reply

  • Sort: 
avatar image
0

Answer by mangosauce_ · Mar 19, 2021 at 07:00 PM

A simpler way to do what you're trying, while also ensuring transforms aren't referenced more than once, is to use GetComponentsInChildren.


             // This retrieves children, grandchildren, great-grandchildren, etc.
             Transform[] children = GetComponentsInChildren<Transform>();
             foreach (Transform child in children)
             {
                 // Execute code with child
             }



Otherwise you can store the child Transforms already collected in a List, then check if the child has already been added to the list before performing a function.

Comment
Add comment · Show 11 · 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 Asaioki · Mar 19, 2021 at 07:11 PM 0
Share

Would for example GetComponentsInChildren() also get 2 box colliders if a single child has 2 of that component attached?

avatar image mangosauce_ Asaioki · Mar 19, 2021 at 07:18 PM 0
Share

I believe it would, yes. If you're looking to get a specific box collider (or any component type you have multiple of attached to a single game object) you might need to get creative and attach a script to the object that can resolve which component to return.


For example, instead of GetComponentsInChildren(BoxCollider) you might use GetComponentsInChildren(BoxColliderResolver), which would be a script attached to game objects with multiple box colliders. Then you can retrieve the correct collider by calling a method like _colliderResolver.Resolve().


Just a thought though, you probably won't need to get that complicated with it. :P

avatar image Asaioki mangosauce_ · Mar 19, 2021 at 07:20 PM 1
Share

Nah I think GetComponentsInChildren is what I am looking for then, previously I was using this function in combination with GetComponents.

Show more comments
avatar image Hellium · Mar 19, 2021 at 08:23 PM 1
Share

Note:

GetComponentInChildren returns the components attached to the gameObject and the children.

https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html

avatar image Asaioki Hellium · Mar 19, 2021 at 08:29 PM 0
Share

ah good thing to keep in $$anonymous$$d

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

114 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

Related Questions

Parent/Player teleports but its children dont teleport aswell 4 Answers

FInd the min and max position of children? 1 Answer

Parent rotates around childs location 1 Answer

find ALL children of a parent 1 Answer

Does a child transform take the name of its parent in unity 3D? 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