Destroy a instantiate object problem

Im trying to destroy my instantiate bullets after a certain time tried

Destroy (gameObject, 5);
but what seems to happen is it destroys my original object and does not allow me to instantiate them anymore

Also tried putting the destroy object script on the object that the bullet collides with but that just throws up the error below

NullReferenceException:the prefab you want to instantiate is null .

any ideas

CODE

var projectile : Rigidbody;
var speed = 20;
var ammoCount = 10;
var lastShot = 0.0;
var reloadTime = 0.5;
var lightonnoff;

function Update () {

if (Input.GetKeyDown (“space”)) {
if ( ammoCount <= 0)
return;//stops the code in its tracks when the above conditions are met

lastShot = Time.time;
ammoCount–;
var instantiatedProjectile : Rigidbody = Instantiate (projectile,

transform.position,
transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,-50,speed));///makes the bullets fire thewaythe ship is pointing
Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
//gravity down makes ball arc
Physics.gravity = Vector3(0, 0, 20);

} }

try Destroy(InstantiatedProjectile.gameObject, 5);

something like that

Ray

Cheers bud that did the trick I actually tried that before but had a syntax error when doing so I am slowly getting there Cheers for the help bro much
Appreciated

In fact cheers to all the Unity Guys out there who help out Newbies !!
Look your making me well up pass me hankerchief

:smile:

When you post code here on the forum, wrap it in code tags. It gets much more readable that way.

ie. (without the extra space in the tag)
[ code]
(code goes here)
[/code ]

turns into:

(codes goes here)

sounds like a plan :smile:

sounds like a plan

Hallo,

i have the same problem, but the code from yellowlabrador:

Destroy(InstantiatedProjectile.gameObject, 5);

caused the Error: Unknown Identifier “InstantiatedProjectile”

How did you get it working ?

Hi bud
just added the line of code he suggested on the bottom of my script and it worked fine I think thats all i did but cant remember for sure

 var projectile : Rigidbody;
var speed = 20;
var ammoCount = 10;
var lastShot = 0.0;
var reloadTime = 0.5;
var lightonnoff;



function Update () {


if (Input.GetKeyDown ("space")) {
if ( ammoCount <= 0)
return;//stops the code in its tracks when the above conditions are met





lastShot = Time.time;
ammoCount--;
var instantiatedProjectile : Rigidbody = Instantiate (projectile,


transform.position,
transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,-50,speed));///makes the bullets fire thewaythe ship is pointing
 Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
//gravity down makes ball arc
Physics.gravity = Vector3(0, 0, 20);
 Destroy(instantiatedProjectile.gameObject, 3);//destroys the cannon bullets after 3 seconds
 
   } }

ah, first i tried to write an own script for the destroytime, but now i put it into my bulletlauncherscript and it works, thanks for the help !
:smile: