Quantcast
Channel: Questions in topic: "gun"
Viewing all articles
Browse latest Browse all 592

Firing bullets ( Tnx for the help )

$
0
0
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 )

Viewing all articles
Browse latest Browse all 592

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>