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 /
  • Help Room /
avatar image
0
Question by uoa7 · Feb 21, 2017 at 09:10 PM · rigidbodyinstantiatedestroy

Trying to destroy instances of Rigidbody2D

Been trying lots of things but no joy trying to Destroy individual instances of a cloned Rigidbody2D.

Error: MissingReferenceException: The object of type 'Rigidbody2D' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. BallEmitter.Update () (at Assets/BallEmitter.cs:18)

Please can someone explain why this happening. Do I need to instantiate a new gameObject for each Rigidbody2D or something? Thanks.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class BallEmitter : MonoBehaviour {
 
     public Rigidbody2D ball;
     public float max;
     public float min;
     public List<Rigidbody2D> balls = new List<Rigidbody2D>();
 
     void Start () {
         InvokeRepeating ("ThrowBall", 0.0f, 0.8f);
     }
 
     void Update () {
         foreach (var ball in balls) {
             if (ball.transform.position.y < -4) {
                 Destroy (ball.gameObject);
             }
         }
     }
 
     void ThrowBall() {
         Rigidbody2D ballInstance;
         ballInstance = Instantiate (ball, transform.position, transform.rotation) as Rigidbody2D;
         ballInstance.isKinematic = false;
         ballInstance.AddForce (-Vector3.right * Random.Range(min,max));
         balls.Add (ballInstance);
     }
 }
 
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
1
Best Answer

Answer by HenryStrattonFW · Feb 21, 2017 at 09:23 PM

This is because you are destroying the rigidbody components but not removing them from the list, so the next frame occurs, you are still trying to delete components that no longer exist.

Another note is that this code destroys the rigid body components and not the game object, so you may want to swap it to destroy the game object instead otherwise you will end up with logs of game objects in your scene that are never removed.

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 uoa7 · Feb 21, 2017 at 10:26 PM 0
Share

Thank you very much @HenryStrattonFW! That was foolish of me, I should have checked the line number - I thought it was unable to instantiate further game objects. But unless I am mistaken Destroy(ball.gameObject) should destroy the gameobject rather than the rigibody.

avatar image uoa7 · Feb 21, 2017 at 10:39 PM 0
Share

For anyone else viewing this question, don't forget to run the for loop in reverse when removing list items:

         void Update () {
             for (int i = balls.Count - 1; i > 0; i--) {
                 if (balls [i].transform.position.y < -4) {
                     Destroy (balls [i].gameObject);
                     balls.RemoveAt (i);
                 }
             }
         }

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

123 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

Related Questions

Bullet clones will not be destroyed. 2 Answers

destroying instantiated groups of objects 1 Answer

Is there any good sample code for shooting a projectile and collision detection? 0 Answers

Unable to delete instance of GameObject 0 Answers

Destroy GameObject in list then instantiate GameObject not working 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