- Home /
Question by
C-Screator · Jan 13, 2013 at 07:53 PM ·
destroybubbles
destroy bullets script!
Im making a shooting game and i really need to add to my script to make the bullets disappear in 2 seconds
var bullet : Transform; //Your bullet prefab.
var speed : float = 10.0f;
var muzzlePoint : Transform; //Your SpawnPoint
function Update() {
if(Input.GetButtonDown("Fire1")) {
var instance : Transform = Instantiate(bullet, muzzlePoint.position,
muzzlePoint.rotation);
instance.velocity = muzzlePoint.forward * speed;
}
}
could anyone please edit this script for me! thanks!
Comment
I started on this, but then i saw you allways have questions like: "Do this or that for me...". Not to mention you never acceped an answer.
Answer by darthbator · Jan 13, 2013 at 09:11 PM
There are more efficient ways of doing this (like monitoring the number of existing bullets in the scene and culling them when over a thresh hold). But for what you're asking you would want to use a coroutine. You may need to gate it with a bool in the update method. This is in C# since my JS is junk
IEnumerator cleanBullets () {
isReady = false;
//Destroy bullet code here
yield return new WaitForSeconds(2f);
isReady = true;
}
then in update you would want something like
if (isReady) StartCoRoutine(cleanBullets());
Your answer
![](https://koobas.hobune.stream/wayback/20220613090903im_/https://answers.unity.com/themes/thub/images/avi.jpg)