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 Presipope · Nov 29, 2012 at 04:54 AM · gameobjectinstantiatedestroy

Destroy Instantiated GameObject Problem

Ok so I'm pretty sure my problem is I'm not using the correct syntax for telling the Destroy command what to actually destroy. I'm just putting a small snippet of the script up because the rest is really unnecessary. The "shotgunShootParticle" variable is a GameObject. Really my only problem is figuring out how to destroy the instantiated object.

 function Update(){
    if(Input.GetButtonDown("Fire1")){
    
        Instantiate(shotgunShootParticle, transform.position, transform.rotation);
        
        Destroy(shotgunShootParticle, 1);
        
     FireWeapon();
     
     }
     
 }
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 CodeMasterMike · Nov 29, 2012 at 06:10 AM 0
Share

What is the problem you have with the function? Do you get a error message or anything?

Because using the Destroy-function should be enough to destroy you gameobject.

The function you have above waits for 1 second before its destroyed, The actual destruction of the object is delayed to after the engine update call is finished.

But I noticed that you don't keep the instantiated object, and you are destroying the orginal object. $$anonymous$$aybe you rather want to destroy the newly instantiated object?

Another thing, what you can also do, is something like this to make sure that the object actually do exist:

   newCreatedShotgunparticle = (GameObject)GameObject.Instantiate(shotgunShootParticle, transform.position, transform.rotation);

    if(newCreatedShotgunparticle != null)
    {
      Destroy(newCreatedShotgunparticle, 1);
    }

But using instantiate in game-mode is not recommended. Try to use a pre-created pool of shotgunparticles ins$$anonymous$$d.

Read more here: http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

Good luck!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Nov 29, 2012 at 07:00 AM

Two solution to your problem:

first: When you destroy an object you need a reference, you do not have any there.

 var myRef = Instantiate(shotgunShootParticle, transform.position, transform.rotation);
 Destroy(myRef, 1f);

Second solution is to simply tick OneShot in the particle system. That will automatically destroy the object.If you are using shuriken tick off Looping.

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 Presipope · Feb 13, 2013 at 06:56 PM 0
Share

Thank you sir! Sorry I didn't accept this answer sooner! I feel so rude I implemented your solution a long time ago! Going back through all my questions right now and giving people their due justice.

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

12 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Trash Collection For Instantiated Game Object 1 Answer

References to GameObject become null 1 Answer

How to instantiate a prefab after its been destroyed 1 Answer

Trouble with destroying an instantiated prefab 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