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 Asaioki · Mar 19, 2021 at 09:55 PM · destroyparentchildchildren

Destroyed child still is referenced.

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? (see comments in code for explaination on the issue):

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CodesCove · Mar 19, 2021 at 11:34 PM

I didn't analyze the code too much but just to note that when you Destroy object you just mark it to be destroyed and it will show as destroyed in the next frame, not the current one. Use DestroyImmediate() to do right away. Read here some considerations when using it https://docs.unity3d.com/ScriptReference/Object.DestroyImmediate.html

Also note: You should do the Destroy object after you run any code that might still reference it in the same frame, that way you don't end up in the situation like this.

Comment
Add comment · Show 2 · 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 11:51 PM 0
Share

Thanks this solved it, I never knew about DestroyImmediate! Never had to use it before.

avatar image CodesCove Asaioki · Mar 19, 2021 at 11:56 PM 0
Share

Great to hear!

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

117 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

Related Questions

Make a simple tree 1 Answer

Create multiple instances of an object 2 Answers

Is it possible to use OnMouseDown(collision other) ??? 1 Answer

Update Parent/ Children From Script? 2 Answers

destroy Parent when child is destroyed 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