• 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
Question by Aunt Jemima · Aug 06, 2012 at 09:32 PM · c#collisionrigidbodydestroy

The object of type 'Rigidbody' has been destroyed but you are still trying to access it.

I have a basic missile launcher set up. The collision works fine, the explosion animation plays when the missile collides with somet$$anonymous$$ng, and then it gets destroyed. The only problem is, I can't shoot any more missiles after the first one!

I have a prefab named "missile" with a rigidbody attached, w$$anonymous$$ch is associated with the projectile variable from MissileLauncher. Is there another way to instantiate a new projectile after destroying the previous gameObject?

 public class MissileLauncher : MonoBehaviour
 {
     public Rigidbody projectile;
     int speed = 20;
  
     void Update ()
     {
         if (Input.GetButtonDown("Fire1"))
         { 
             projectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation); //Instantiate projectile
             projectile.velocity = transform.TransformDirection(new Vector3(0,0,speed)); //Set projectile velocity
  
             Physics.IgnoreCollision(projectile.collider, transform.root.collider); //Avoid projectile an hero
         }
     }
 }

 public class Projectile : MonoBehaviour
 {
     public GameObject explosion;
  
     void OnCollisionEnter(Collision collision)
     {
         ContactPoint contact = collision.contacts[0];
         transform.rotation = Quaternion.FromToRotation (Vector3.up, contact.normal);
  
         explosion = (GameObject)Instantiate(explosion, contact.point, transform.rotation);
  
         Destroy(gameObject);
     }
 }

 public class Explosion : MonoBehaviour
 {
     float explosionTime = 0.25f;
  
     void Start ()
     {
         Destroy (gameObject, explosionTime);
     }
 }
Comment

People who like this

0 Show 3
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 RodrigoSeVeN · Aug 07, 2012 at 12:02 AM 0
Share

Everything seems to be in order as far as I get it, since I only work with javascript. Besides that, you should be able to virtually instantiate infinite copies of a prefab. Can you instantiate more than one prefab at a time before the first one exploding? Also, how are you trying to tell the hero that it's ok to shoot again?

avatar image Bunny83 · Aug 07, 2012 at 12:36 AM 0
Share

@sevenrmn:
He uses GetButtonDown, so the player can shoot as fast as he can press the button (which is only limited by the frames per second) ;)

avatar image RodrigoSeVeN · Aug 07, 2012 at 12:41 AM 0
Share

@Bunny83 My question was actually answered by your answer, as he can't instantiate more than one because he is disposing of the prefab variable. I'll have to get more familiar with C# again so I can pinpoint these kinds of details.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Aug 07, 2012 at 12:32 AM

Instantiate will return the object it instantiates. You overwrite your original prefab reference with your instance. When t$$anonymous$$s instance get destroyed you don't have any source object you can Instantiate.

So, just do it like t$$anonymous$$s:

     // [...]
     if (Input.GetButtonDown("Fire1"))
     {
         Rigidbody clone = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
         clone.velocity = transform.TransformDirection(new Vector3(0,0,speed));
         Physics.IgnoreCollision(clone.collider, transform.root.collider);
     }
     // [...]
Comment
RodrigoSeVeN

People who like this

1 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 Aunt Jemima · Aug 07, 2012 at 12:48 AM 0
Share

That does it, thanks!

avatar image

Answer by drak0 · Aug 07, 2012 at 01:07 AM

I don't have experiente with C# but from what I see, you are trying to acces the same object that you destroy in the Projectile script.Try t$$anonymous$$s : declare a new variable to hold a reference of the instantiated projectile ( you are using the same identifier, "projectile" for the projectile prefab that you instantiate, and also for the variable that holds the reference to that new instance).

public class MissileLauncher : MonoBehaviour { public Rigidbody projectile; public Rigidbody spawnedProjectile; int speed = 50;

 void Update ()
 {
     if (Input.GetButtonDown("Fire1"))
     { 
         spawnedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation); //Instantiate projectile
         spawnedProjectile.velocity = transform.TransformDirection(new Vector3(0,0,speed)); //Set projectile velocity

         Physics.IgnoreCollision(spawnedProjectile.collider, transform.root.collider); //Avoid projectile an hero
     }
 }

}

T$$anonymous$$s should work for you.

Comment

People who like this

0 Show 0 · 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

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.

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

10 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

Related Questions

Objects with colliders going through walls and each other. 3 Answers

Prevent a specific RB to influence another, but still collide with everything 0 Answers

Multiple Cars not working 1 Answer

Rigidbody collision is delayed[FIXED] 0 Answers

Making Objects and Piling them up , 0 Answers


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