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 zombieninjajack · Jan 06, 2014 at 01:45 AM · gun

Bullets dont delete properly

the bullets spawn and launch properly but the problem is that i have a destroy object script for 5 second which it works, after 5 seconds of the bullet being spwned it dletes itself the problem is that when it deletes the final bullet that spawned unless i keep firing, it wont spawn anymore bullets saying that Object type rigid body has been destroyed but im still trying to access it. how do i fix it hers a video :http://www.youtube.com/watch?v=ZuhAnaPowVs&feature=youtu.be

and my spawn script

 #pragma strict
  
 var Bullet : Rigidbody;
 var Spawn : Transform;
 var BulletSpeed : float = 1000;
 
 
 
 function Start () {
 
 }
 
 function Update () 
 {
     if(Input.GetButtonDown("Fire1")){
         Fire();    
     }
 }
 function Fire(){
         Bullet = Instantiate(Bullet,Spawn.position,Spawn.rotation);
         Bullet.AddForce(Bullet.transform.forward*BulletSpeed);
         audio.Play();
     }

then delete script

 Destroy (gameObject, 5);


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ncallaway · Jan 06, 2014 at 01:50 AM

The problem is that when you instantiate a new bullet, you store it over the variable that you use to instantiate the next bullet.

What this means is that when the last bullet is destroyed, your Bullet variable now refers to a destroyed rigidbody. When you next attempt to instantiate a bullet, you pass Instantiate() destroyed object. To fix this, you'll want to have a separate variable that stores the newly instantiated bullet.

 function Fire(){
     var newBullet : Rigidbody = Instantiate(Bullet, Spawn.position, Spawn.rotation);
     newBullet.AddForce(Bullet.transform.forward * BulletSpeed);
     audio.Play();
 }
Comment
Add comment · Show 6 · 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 zombieninjajack · Jan 06, 2014 at 02:11 AM 0
Share

omg it works thanks for the in depth reasoning to, actually it works but when i rotate my charcter from the original position ie look to the left the bullet comes out the gun going sideways trying to go in the same direction what the problem?

avatar image ncallaway · Jan 06, 2014 at 02:18 AM 0
Share

No problem! If it works, you might consider marking the answer as the accepted answer.

Accepting answers makes it easier to get your future questions answered. :)

avatar image sdgd · Jan 06, 2014 at 02:19 AM 0
Share

seriously you should learn to accept answers

avatar image zombieninjajack · Jan 06, 2014 at 03:10 AM 0
Share

when i rotate my charcter from the original position ie look to the left the bullet comes out the gun going sideways trying to go in the same direction what the problem?

avatar image ncallaway · Jan 06, 2014 at 03:55 AM 0
Share

You are using the Spawn variable to deter$$anonymous$$e each fired bullet's position and rotation. Whatever the rotation of "Spawn" is at the time of the Instantiate() call will be the rotation of the new bullet.

Without knowing more about your project, I can't really help debug why Spawn has the incorrect rotation. If I had to hazard a guess, I would say that Spawn should be a child of your character object so that when the character rotates, Spawn automatically rotates.

It's kinda a different question though, and the comments aren't really the appropriate forum for more questions, so if that doesn't get you to an answer, I'd recommend creating a new question...

Show more comments

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Problem doing a script. 2 Answers

Script suddenly stopped working properly 1 Answer

Animation + Scripting = I Don't Understand (Help) 0 Answers

Bullet Hole not inline with sight?(Center of Screen) 1 Answer


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