- Home /
i have too many bullets...
i am using some scripts that i found that make it so I can shoot. It uses a variable that is the ball, and it spawns it to the empty gameobject that shoots it. here is the website I got it from: https://mega.nz/#F!dkklTQiR!WL25e1TKLmhYKQT2RpIXNA (you will have to sign in, i just used blur for fake account though) it lets me shoot, but in the files it gives me two choices: either gives me a certain amount of time I can keep the bullet and it will destroy all the clones with it, or it will keep all the clones and keep the bullet. for ease of use, here is the shoot program that makes the clones: static var ammo = 100; static var maxAmmo = 100; var key : String = "mouse 0"; var bullet : Rigidbody; var speed : float = 1000;
function Update () {
if(Input.GetKeyDown(key)){
if(ammo > 0){
shoot();
}
}
}
function shoot(){
var bullet1 : Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
bullet1.AddForce(transform.forward * speed);
ammo --;
}
I need a way to destroy the clones and keep the main bullet. thanks!
Answer by Cornelis-de-Jager · Jul 12, 2017 at 09:58 PM
Simply add the following bit to the script:
float time = 5f; // Time it takes after spawn for bullet to be destroyed
void Start () {
Destroy (this.gameObject, time)
}
You can then also destroy the bullet when it collides with an object
void OnCollisionEnter (Collision other) {
Destroy (this.gameObject);
}
Answer by Steel598 · Jul 12, 2017 at 10:05 PM
@Cornelis-de-Jager do I put the float time stuff at function update? thanks for that tidbit though! edit: it also gives me these error messages: Assets/gun script/Unity gun scripts/shoot.js(9,37): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/gun script/Unity gun scripts/shoot.js(6,7): UCE0001: ';' expected. Insert a semicolon at the end.
another edit: fixed one of these errors, but the other one says it needs a semicolon even though it has one. thanks, i got the place too.
like it says, I missed a semi-colon. Just add it:
Destroy (this.gameObject, time);
And the float needs to be outside all functions, but still within the class.
Oh btw! this should be added to a script that is attached to the bullet ins$$anonymous$$d of the script that actually spans the bullet. Sorry about confusion!!!!