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 /
avatar image
0
Question by aaeriam · Nov 22, 2015 at 09:41 AM · c#destroybulletsgun script

How do I destroy a gun bullet clone?

Hey everyone! I know for a fact this question has been asked before; however, the other forum pages I looked at didn't have the answer I was looking for so here goes!

Just like the topic, I've got a perfectly fine gun script that shoots without any problems. Unfortunately, I'm still pretty new to scripting and Unity and I've been having problems understanding why my bullet clone isn't being destroyed.

using UnityEngine; using System.Collections;

public class Shoot : MonoBehaviour {

 public Rigidbody rocket;
 public Rigidbody n_rocket;
 public float speed = 10f;
 public float destroySpeed = .5f;

 void FireRocket ()
 {
     Rigidbody n_rocket = (Rigidbody)Instantiate(rocket, transform.position, transform.rotation);
     n_rocket.velocity = transform.forward * speed;
 }

 void Update ()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         FireRocket();   
     }
     Destroy(n_rocket, destroySpeed);
 }

}

Now it works perfectly fine if I write the Destroy script like so:

Destroy (GameObject.Find("Bullet (Clone)"), destroySpeed);

however, from what I've seen written on the forums, using GameObject.Find can be inefficient for something like this. So why does the GameObject.Find work but it doesn't destroy the object if I reference it by it's name, n_rocket?

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 Mich_9 · Nov 23, 2015 at 03:43 AM 0
Share

Is FireRocket(); called constantly? If so, you should implement some kind of pooling system, Instantiating gameobjects can be resource intensive depending how you implement it. Otherwise you can do like Cherno's answer.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Cherno · Nov 22, 2015 at 11:39 AM

You declare a reference to the instantiated rigidbody called "n_rocket". Later, you use the Destroy function to destroy the referenced object, which is still a rigidbody, so only the rigidbody component of the gameobject is destroyed, not the whole GameObject.

The solution is of course to not instantiate a rigidbody, but rather a whole GameObject (which has a rigidbody you can access via GetComponent), and destroy this instead.

Edit: Or do it like this:

 Destroy(n_rocket.gameObject, destroySpeed);

Comment
Add comment · 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
0

Answer by m0guz · Nov 22, 2015 at 05:48 PM

@Cherno is right. Also you can use n_rocket.gameObject when destroying which references to object not component.

Comment
Add comment · 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
0

Answer by MewEight · Nov 23, 2015 at 03:50 AM

My personal method would be just to use a coroutine.

  void FireRocket ()
  {
      Rigidbody n_rocket = (Rigidbody)Instantiate(rocket, transform.position, transform.rotation);
      n_rocket.velocity = transform.forward * speed;
      StartCoroutine(DestroyLater(n_rocket.gameObject, destroySpeed);
  }

     private IEnumerator DestroyLater(GameObject obj, float delay)
     {
         yield return new WaitForSeconds(delay);
         Destroy(obj);
     }



Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Make make gameObject invisible until collision with bullet 1 Answer

Destroying an object from its script. 1 Answer

Trouble with destroy function 2 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