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 pulkit8.mahajan · Mar 22, 2014 at 07:36 PM · shootinghow-todestroy-clones

How to destroy bullets?

So,I know many people have already asked this question already, but I haven't found any good answers. I have a gun object attached to my player, and am trying to destroy the bullets after they reach a certain point. But after some point, it gives me an error :

MissingReferenceException: The object of type 'GameObject' 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. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) JoystickShoot.fire () (at Assets/JoystickShoot.js:57) JoystickShoot.Update () (at Assets/JoystickShoot.js:34)

I dont know why it gives me the error. The prefab is set to the variable bullet, which is being instantiated. Here is the destroying code:

                        if(this.transform.position.x > 10) {
             Destroy(this.gameObject);
             }
             if(this.transform.position.z > 10){
             Destroy(this.gameObject);
             }
             if(this.transform.position.x < -10){
             Destroy(this.gameObject);
             }
             if(this.transform.position.z < -10){
             Destroy(this.gameObject);
             }

Please Help!

Comment
Add comment · Show 6
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 ShadoX · Mar 22, 2014 at 07:39 PM 1
Share

Not sure if you actually need a gameObject for a script to still be active, so perhaps, one of the if checks destroys the gameObject while another if tries to do the same later, which obviously can't be done if it already has been destroyed before. (as in - the gameObject which has the script attached already destroyed the gameObject, but the script is still running)

so perhaps you have to also destroy the instance of the script?

Have you tried putting Debug.Log(...) messages after each Destroy and check if some of them happen to fire more than once for 1 object ?

avatar image pulkit8.mahajan · Mar 23, 2014 at 08:54 PM 0
Share

how would I go about detroying the instance of the script?

avatar image robertbu · Mar 23, 2014 at 09:18 PM 0
Share

Can you post the entire JoystickShoot.js script? I don't think the error is the one ShadoX indicated. If a script is a component on a game object, the script dies with the object.

avatar image FinKone · Mar 23, 2014 at 11:58 PM 0
Share

Well if you attach a small script to the prefab bullet you are instantiating, and after a certain duration you could destroy the object itself with the script attached to the bullet gameobject. It would make it easy to on a time base as compared to a distance base thing.

avatar image pulkit8.mahajan · Mar 24, 2014 at 04:50 AM 0
Share

the time thing is not working either.

Show more comments

2 Replies

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

Answer by RyanZimmerman87 · Mar 24, 2014 at 11:47 PM

So I just realized one last detail after I typed all this other stuff below which might be worthless.

You are only using one bullet GameObject variable. And you are instantiating the bullet from itself. I think you should try using two gameObject variables for this process.

Use one to hold the initial bullet prefab, this one could easily just be a public variable so you can set it super easy to the right prefab in the editor.

And then you can create a newBullet variable for all your instantiated bullets. That's the way I've always done it for anything like bullets and projectiles.

So for example:

 //set this to your bullet prefab in Editor
 public GameObject bulletPrefab;
 
 //then in your fire() function
 //create a new bullet
 
 void fireFunction()
 {
 GameObject newBullet;
 newBullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
 }

That would eliminate the possibility of having any kinds of conflicts with your variables. Right now you may be trying to instantiate or destroy the wrong bullets since you only have one bullet variable in your entire script. You are replacing the original with the instantiated one so things may get messy.

I've always used 2 GameObject variables for this kind of cloning for projectiles so I'm pretty sure you can fix it that way pretty easily.

Here's my original answer ideas before I realized what I wrote above:

So is everything working except it's just giving you that exception error?

You might wanna try using:

else if ()

After your first condition check instead of having them all be if()

If each bullet has that script on them within Update() I don't see what could be the problem unless somehow more than one:

If()

Check is executing before the deletion occurs. Maybe some editor lag could cause this if that's even possible? I was under the impression using Destroy() would immediately occur so reaching the next if() statement would be impossible.

I'm assuming you have the script in your original post attached correctly for reach bullet prefab that you instantiate and it's in the Update() function.

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 SkaredCreations · Mar 24, 2014 at 12:20 AM

Are you sure that you are instantiating the bullet from a prefab (in your project folders) and not from a GameObject living in the scene (that is being destroyed)? The error stack trace looks like you're trying to instantiate a GameObject that has been destroyed.

Comment
Add comment · Show 3 · 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 pulkit8.mahajan · Mar 24, 2014 at 04:36 AM 0
Share

Yes i am sure i am instantiating a prefab.

avatar image SkaredCreations · Mar 24, 2014 at 09:23 PM 0
Share

Can you add to your topic the content of the functions JoystickShoot.fire and JoystickShoot.Update? This is most likely a game logic issue.

avatar image pulkit8.mahajan · Mar 24, 2014 at 11:28 PM 0
Share

Both are posted in the comments above

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

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

How add this destory bullet to my autofire script? 1 Answer

Shooting 2D - how to fix 1 Answer

Cannon shoots in wrong direction with unrealisic Physic 1 Answer

AirStrike Scripting Help 0 Answers

how to make enemies shoot projectiles towards the players corrent position?(2d game) 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