- Home /
Reloading Function in Script not doing anything.
My game runs and I don't get any errors of any kind, but I can't seem to get my reloading function to work. Nothing happens when I press the "r" key. I think it has to do with me calling the reload function in a spot the game can't detect, but I googled my problem dozens of times and I can't come up with why it wouldn't work calling it in the Update. Any advice/suggestions would be appreciated.
var speed = 10;
var maxbullets : int = 6;
var totalBullets: int = 12;
var reloadtime: int = 2;
var transfer : int = 6 - maxbullets;
function Update() {
Fire();
Reload();
}
function Fire() {
if (maxbullets > 0);
if(Input.GetButtonDown("Fire1"))
{
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
Destroy(clone.gameObject, .25);
maxbullets -= 1;
GameObject.Find("BulletCount").guiText.text = ""+maxbullets;
}
}
function Reload() {
if(totalBullets > 0);
if (transfer > totalBullets) { transfer = totalBullets; }
if(Input.GetKeyDown("r"))
yield WaitForSeconds (reloadtime);
animation.Play("RevolverReloadAnimation");
{
maxbullets += transfer;
totalBullets -= transfer;
GameObject.Find("TotalBullets").guiText.text = ""+totalBullets;
}
}
Thanks!
Answer by GameVortex · Feb 04, 2014 at 07:41 AM
The curly braces for the if check with the Input.GetKeyDown("r") is not correctly set up. Without the curly braces the only thing happening when you hit the button is the yield statement. The animation.Play is running each update which means the animation begins at the beginning every frame, and therefore you do not see anything happening.
You need to move the starting curly brace from line 40. to line 37, and you would probably want to play animation before the yield or nothing will happen before the reload time is up.
I did everything you suggested and I also tried moving the if(Input.Get$$anonymous$$eyDown("r")) to the update function, none of it seemed to work. thanks for the advice on the braces though, I guess I didn't see the problem there.
Then have you set up the "r" key in the **Input$$anonymous$$anager**? Any keys you try to get through a string name needs to be set up there.
Try just for testing to use Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.r) ins$$anonymous$$d. Also put a debug.log statement inside the if check to make sure that it is working.
I might be wrong but Get$$anonymous$$eyDown("r") doesn't have to be setup in the Input $$anonymous$$anager.
You are correct, my bad, I was of course thinking about ButtonDown A debug after the input would still probably be wise to check if the input is detected properly.
Answer by Nanobrain · Feb 04, 2014 at 06:49 AM
if(Input.GetKeyDown("r"))
should go in update(). Your input check is never being called. That is unless Reload() is being called elsewhere in your code, and on every game loop.
function Update() {
Fire();
Reload();
}
Reload() is being called every frame, so in theory that should work just fine.
OrangeLightning, very true, very true. I overlooked that fact.
Your answer
Follow this Question
Related Questions
is it possible to use a rotation on a Vector3? 3 Answers
accessing a function? 1 Answer
How do I make a callback run? 1 Answer
Weirdness.. Why does putting yield in function corrupt commands? 1 Answer
problem with calling a function from THIS script, on an object that I find with THIS script. 1 Answer