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 )
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);
}
}
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
Follow this Question
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