• 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 Myles Jones · Jul 13, 2014 at 05:11 PM · instantiatemultiple

Trouble destroying instantiated object

I am instantiating a projectile and trying to have it delete after 3 seconds. I have been using this script:

 #pragma strict
 
 var prefabBullet : Rigidbody;
 var shootForce : float;
 var shootPosition : Transform;
 
 
 function Update () {
 
     if(Input.GetMouseButtonDown(0)){
         var instanceBullet = Instantiate(prefabBullet, transform.position, shootPosition.rotation);
         instanceBullet.rigidbody.AddForce(shootPosition.right * shootForce);
         Destroy(instanceBullet, 3.0);            
     }
 }
 

...But it doesn't work. I don't know why it isn't as I have not done much programming, and I haven't done it in a long time (took a big break from Unity).

I have 2 cannons, that both instantiate/clone the same projectile and shoot it. I have tried other methods such as using Destroy(gameObject.Find("projectile")), which did seem to do the job in a way, but for some reason it would only delete one of the two instantiated projectiles.

Any help is appreciated!

Comment
Add comment · Show 4
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 Tehnique · Jul 13, 2014 at 05:23 PM 0
Share

GameObject.Find returns only 1 object, always. You could use GameObject.FindGameObjectsWithTag to find all projectiles (you'd have to tag them first).

avatar image Myles Jones · Jul 13, 2014 at 05:41 PM 0
Share

When I try that I get the error "No appropriate version of 'UnityEngine.Object.Destroy' for the argument list '(UnityEngine.GameObject[])' was found."

avatar image Tehnique · Jul 13, 2014 at 06:39 PM 0
Share

Yes, because FindGameObjectsWithTag returns an array (a list). Run a foreach on that list and in that foreach call your destroy code. This solution is not the best resource-wise, the initial solution is better, so maybe you should try to make the script you posted work without Find if you see any performance issues.

avatar image Kiwasi · Jul 13, 2014 at 06:42 PM 0
Share

Don't use find in this case. Its a really bad idea and is asking for trouble.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Jul 13, 2014 at 06:41 PM

This is the correct way to do it. Instantiate will always return an Object, not a GameObject, so you might be having a typing issue (Hence you should use C# and not JavaScript.) Try the following edit:

 var instanceBullet = Instantiate(prefabBullet, transform.position, shootPosition.rotation) as GameObject;

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 Mehrdad995 · Jul 13, 2014 at 06:55 PM

create a script and write the destroy cod in it's Awake function then attach it to your projectile prefab. in this way when your projectile instantiate the awake function of it will run which will destroy it after the given time.

write this cod in destroyer script attached to your projectile I hope it gonna works.

 var delay : flaot; // your prefab lifetime
 
 function Awake()
 {
  Destroy(transform,delay);
 }
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 Myles Jones · Jul 13, 2014 at 07:07 PM

I have fixed it finally! After the advice, I tried not using .Find, so I tried to continue the use of Destroy(instanceBullet, since I have almost no knowledge on arrays. Adding "as GameObject also didn't work.

The solution was to change:

 Destroy(instanceBullet, 3.0);

to:

   Destroy(instanceBullet.gameObject, 3.0);  

Thanks for all of the advice too!

Comment
Add comment · 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 Kiwasi · Jul 13, 2014 at 07:17 PM 0
Share

And that's why I hate JavaScript

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

24 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

Related Questions

Batching objects with Instantiate for optimization 2 Answers

Checking if object intersects? 1 Answer

Instantiate multiple objects for procedual map 1 Answer

Why does instantiation of clone cause duplicates 1 Answer

How to use many particle systems, just varying in size and emitter dimensions 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