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 /
  • Help Room /
avatar image
0
Question by thinkal vb · Dec 15, 2015 at 01:04 PM · destroybulletgun

Firing bullets ( Tnx for the help )

 using UnityEngine;
 using System.Collections;
 
 public class instantiate : MonoBehaviour
 {   // have a bullet.prefab, and attached it to script 
     public Rigidbody bullet;
     // barrelEnd- an empty object ( attached to the script ) [ in front of the gun ]
     public Transform barrelEnd;
       
     void Update ()
     {   
         if(Input.GetButtonDown("Fire1"))
         {   Rigidbody bulletInstance;       
             bulletInstance = Instantiate(bullet, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
             bulletInstance.AddForce(barrelEnd.forward * 5000);
             // to distroy the bullet after 5 seconds 
             Destroy(bulletInstance,5.0f);
         }
                     
     }
     
 }



The code is from an unity tutorial :-

https://unity3d.com/learn/tutorials/modules/beginner/scripting/on-mouse-down?playlist=17117 I tried to do the same as told in the video.


But when i play the game, soon after the 1.0f time, the bullet prefab itself getting destroyed and couldn't fire the weapon


Error:- MissingReferenceException: The object of type 'Rigidbody' 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) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:60) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:80) instantiate.Update () (at Assets/Scripts/instantiate.cs:20)


so i changed the code like this and :- I was able to fire but as i fire bullets more bullet instances are formed, but nothing is getting destroyed ( they all pile up in my hierarchy )

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
Best Answer

Answer by lubbydog · Dec 16, 2015 at 03:22 AM

So if i am correct, you can shoot the bullets but they stay in the scene forever. What you need to do is add a timer like this and attatch it to the bullet prefab.

//Time the object will be in the game (in ticks).

var timeIn = 5000;

function Update(){

//Deletes 1 from the timeIn variable every tick.

timeIn -= 1;

if(timeIn <= 0){

//Destroys the game object if the timeIn reaches 0.

Destroy(this.gameObject);

}

}

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 thinkal vb · Dec 16, 2015 at 01:05 PM 0
Share

Thanks for the replay; ( $$anonymous$$erry Christmas ) I will surely give your trick a try. I already figured it out :- I attached the bullet prefab to my script from the hierarchy. Since the bullet prefab is on the hierarchy, it itself get destroyed after 5 seconds.

How I fixed it:- I just put the bullet.prefab in my assets and linked that prefab to my script. So only the cloned bullets are destroyed not the source ( since the prefab is not in the hierarchy, the script attached to it won't get executed). So i can make clones as much as i need. When i clone them- the clones are placed in the hierarchy and thus get destroyed after the required time [because the script attached to them become alive ]

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

34 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MY effect dont destroy 0 Answers

Bullet Destroy 1 Answer

The bullet never destroyed!!! 2 Answers

Destroying Bullet (Impact Effect) 1 Answer

Unity 2d simple c# shooting script 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