- Home /
reload script continues adding the reload amount
i have this script (the fire input that removes a bullet is added into this script): static var load = 8; var seeLoad = 8; var ammo = 100; var reloadTime = 3.0; var pistol : Transform;
function Update () {
if(Input.GetButtonDown("Fire1")){
if(load >= 1){
load -= 1;
}
}
if(Input.GetButtonDown("r")){
reload();
pistol.animation.CrossFade("pistol reload");
}
if(load <= 0){
reload();
pistol.animation.CrossFade("pistol reload");
}
seeLoad = load;
}
function reload () {
yield WaitForSeconds(reloadTime);
load = 8;
}
the problem is that when the 3 seconds are over and the load var is put back to 8 for another 4 seconds if i fire it stays at 8, the waitForSeconds makes it equal to 8 for this time... is there a way to make it equal 8 once then be changeable?
thanks
Answer by Alec-Slayden · Mar 05, 2012 at 09:41 PM
Your code formatted (use the code tag):
static var load = 8; var seeLoad = 8; var ammo = 100; var reloadTime = 3.0; var pistol : Transform;
function Update () { if(Input.GetButtonDown("Fire1")){ if(load >= 1){ load -= 1; } } if(Input.GetButtonDown("r")){ reload(); pistol.animation.CrossFade("pistol reload"); } if(load <= 0){ //check for reloading! reload(); pistol.animation.CrossFade("pistol reload"); } seeLoad = load; }
function reload () { //add reloading = true here! yield WaitForSeconds(reloadTime); load = 8; }
This seems to be the problem: you're calling reload() every frame that load is at 0, including the time that you're WaitForSeconds(reloadTime);
One solution would be to add a boolean called "reloading", and make the load check:
if(load <= 0 && !reloading){
reload();
pistol.animation.CrossFade("pistol reload");
}
Set reloading to true at the start of your reload() function, this way it won't call reload if it has already, and add a condition in update to set it back to false like so:
if(load > 0) reloading = false
Or just put reloading = false after you set load to 8, so you can reload more than once ever :D
Your answer
Follow this Question
Related Questions
How to teleport to a random location in a defined range within a random time span? 0 Answers
Make the animation play at the start of the time waited, not the end of it? 1 Answer
WaitForSeconds in Update() function 4 Answers
WaitForSeconds wont work for me? 0 Answers
Setting Scroll View Width GUILayout 1 Answer