Hi thanks for the reply however that just throws up a error rigidbody destroyed but you are still trying to access it im pretty certain the cloned object has to be referenced its the how is the problem…
Hey there, I’m rather new to Unity but I am doing something similar with the lasers that ships in my game fire. Each laser is a little prefab collider sphere (gravity turned off in the rigidbody) with a point light attached, and each prefab has the following code:
var lifeTime = 1.0;
function Awake ()
{
Destroy (gameObject, lifeTime);
}
function OnCollisionEnter(collision : Collision)
{
Destroy (gameObject, 0);
}
The death timer is self-explanatory, and the collision entry is so that the laser destroys itself whenever it collides with anything.
Works nicely for me, each of my lasers flies wonderfully and destroys itself as desired. Hope that works for you!
thanks for the reply but still throws up a error here is the full code if any one wishes to delve in there that would be fab
var speed = 10;//speed of bullet
var ammo:Rigidbody;//ammo
var currentBullets = 0;//This is the amount RIGHT NOW that the gun has. (should be 0 by default.)
var totalBullets = 100; //TOTAL amount the gun will have
var readynow : boolean = true;
//var emelly : GameObject;
var ammofont: GUIText;
function Start()
{
if(Input.GetKey("space"))
{
if (currentBullets > 0) //only shoot if you have ammo.
{
readynow = false;//can't shoot
var ammoClone : Rigidbody = Instantiate(ammo, transform.position, transform.rotation);
ammoClone.velocity = transform.forward * speed;
yield WaitForSeconds(.1);// pause between shots
readynow = true;//can shoot
currentBullets--; //subtract one
}
else //if you have less than 1 bullet
{
//perform reload animation
readynow = false;//can't shoot
//emelly.animation.CrossFade("flight");//currentBullets = totalBullets; //reset ammo count
//readynow = true;// animation over can shoot
////////
}
}
}
function LateUpdate ()
{
if(readynow)
{
Start();
}
}
function Update ()
{
if (Input.GetKey ("space"))
ammofont.text = currentBullets.ToString();
//if (Input.GetKey ("space"))
//readynow = true;
}
var speed = 10;//speed of bullet
var ammo: Rigidbody;//ammo/was rigidbody change to prefab
var currentBullets = 0;//This is the amount RIGHT NOW that the gun has. (should be 0 by default.)
var totalBullets = 100; //TOTAL amount the gun will have
var readynow : boolean = true;
//var emelly : GameObject;
var ammofont: GUIText;
function Start()
{
if(Input.GetKey("space"))
{
if (currentBullets > 0) //only shoot if you have ammo.
{
readynow = false;//can't shoot
var ammoClone : Rigidbody = Instantiate(ammo, transform.position, transform.rotation);
ammoClone.velocity = transform.forward * speed;
Destroy(ammoClone.gameObject, 1);
yield WaitForSeconds(.1);// pause between shots
readynow = true;//can shoot
currentBullets--; //subtract one
}
else //if you have less than 1 bullet
{
//perform reload animation
readynow = false;//can't shoot
//emelly.animation.CrossFade("flight");//currentBullets = totalBullets; //reset ammo count
//yield WaitForSeconds(2);
//however long your reload animation is
//readynow = true;// animation over can shoot
////////
}
}
}
function LateUpdate ()
{
if(readynow)
{
Start();
}
}
function Update ()
{
if (Input.GetKey ("space"))
ammofont.text = currentBullets.ToString();
//if (Input.GetKey ("space"))
//readynow = true;
}