1

I have the following function to manage the reloading process of my player's gun in my game. I call the function from an AmmoCheck function and it runs, however the if statement is never run because the input doesn't get detected. The line:

Debug.Log("Reload function check");

will print to the console, but nothing further.

I have made sure the Input is set up under Edit>Project Settings>Input, and it is set to the r button. (Proof - https://i.sstatic.net/jMYeZ.png )

The function:

void gunReload()
{
    Debug.Log("Reload function check");
    if (Input.GetButtonDown("Reload")) {

        Debug.Log("Reloading");
        if(changeWeapon.currentGun == changeWeapon.gunPistol) {
            aReload.SetActive(false);
            // Incrementing the aClipPistolCurrent value by 1 so the current clip "should" progress one along? idk
            aClipPistolCurrent += 1;
        }
        if(changeWeapon.currentGun == changeWeapon.gunAssault) {
            aReload.SetActive(false);
            // Incrementing the aClipPistolCurrent value by 1 so the current clip "should" progress one along? idk
            aClipAssaultCurrent += 1;
        }

    }       
}

Function calling gunReload();

gunAmmoCheck() is being called inside of Update()

void gunAmmoCheck()
{

    if(changeWeapon.currentGun == changeWeapon.gunPistol && aClipPistol[aClipPistolCurrent] > 0) {
        gunFire ();
        // Reducing the ammo of the current clip by 1.
        // ClipPistol is being used (say array, why array
        aClipPistol[aClipPistolCurrent] -= 1;
    }
    if(changeWeapon.currentGun == changeWeapon.gunAssault && aClipAssault[aClipAssaultCurrent] > 0) {
        gunFire ();
        // Reducing the ammo of the current clip by 1.
        // ClipPistol is being used (say array, why array
        aClipAssault[aClipAssaultCurrent] -= 1;
    }

    if(aClipPistol[aClipPistolCurrent] == 0)
    {
        Debug.Log ("Reload");
        // Activating the reload notification on the interface
        aReload.SetActive(true);
        gunReload();
    }
    if(aClipAssault[aClipAssaultCurrent] == 0)
    {
        Debug.Log ("Reload");
        // Activating the reload notification on the interface
        aReload.SetActive(true);
        noAmmo = true;
        gunReload();
    }
}

I'm at a loss here, since all of my other inputs work flawlessly. Any help would be greatly appreciated.

6
  • Out of curiosity, does GetKeyDown work? docs.unity3d.com/ScriptReference/Input.GetKeyDown.html Commented May 14, 2015 at 19:48
  • GetKeyDown works, but it only works for a single frame, the state is reset after the frame is complete so it has to be called in the Update method. Check the key state in the update routine and set a flag that is checked in the reload routine.
    – Ron Beyer
    Commented May 14, 2015 at 20:09
  • I'm sorry Ron, but could you explain it in a little more detail?
    – ixTec
    Commented May 14, 2015 at 20:12
  • He hasn't said if this is not coming from Update() If we knew this was coming from a Coroutine, I would agree, but I'm pretty sure this is just a couple calls up the stack from Update(). Commented May 14, 2015 at 20:13
  • Sorry yes, the ammoCheck function that calls the reload function is called in Update().
    – ixTec
    Commented May 14, 2015 at 20:14

1 Answer 1

1

I just tested this and it worked. I am starting to think that the problem is where you are calling the function from. Not sure yet but

Lets find the problem slowly. Can you comment out those things in your function and have only

 void gunReload ()
    {
        //Debug.Log ("Reload function check");
        if (Input.GetButtonDown ("Reload")) {

            Debug.Log ("Reloading");

        }       
    } 

in your code. Then call gunReload () from the Update and tell me if it works. It would also be good to pose the function you are calling gunReload () from.

7
  • Doing what you've said logs the "Reloading" message so the input works for that. Update original question with the function I was calling it from originally.
    – ixTec
    Commented May 14, 2015 at 22:02
  • One more thing. Can you post the line of code in the Update() where gunAmmoCheck() is called. For example, did you call it with gunAmmoCheck() or did you have to check for some logic before you called it. For example, if(...){gunAmmoCheck()} ?
    – Programmer
    Commented May 14, 2015 at 22:34
  • I currently have it under: if (Input.GetButtonDown ("Fire1")) { gunAmmoCheck(); } Which I now realise would probably mean I'd have to have Fire1 down at the same time as my reload button maybe?
    – ixTec
    Commented May 14, 2015 at 22:39
  • Lol yes. that's the problem. Do you really want the player to be firing before they can reload? I haven't seen any game control like this before. Remove the gunAmmoCheck(); from that if statement and check if it is working properly now
    – Programmer
    Commented May 14, 2015 at 22:41
  • /sigh. I'm an idiot, I swear. I must've looked through the script at least twenty times trying to figure out. What would you recommend I do fit in the Fire1 check as well as the reload check?
    – ixTec
    Commented May 14, 2015 at 22:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.