# Reload Gun

**URL:** <https://discussions.unity.com/t/reload-gun/603676>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [November 1, 2015, 4:08am UTC](https://discussions.unity.com/t/reload-gun/603676 "2015-11-01T04:08:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LongformStudios](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/longformstudios/32/157583_2.png) [@LongformStudios](https://discussions.unity.com/u/LongformStudios)\
**Post date:** [November 1, 2015, 4:08am UTC](https://discussions.unity.com/t/reload-gun/603676/1 "2015-11-01T04:08:50Z")

</div>

Hello Unity!

So I’ve been having some difficulties with making a reload system for a gun for my game. I’m trying to add a delay in order to add some sort of affect.

Here’s my code

```csharp
Debug.Log(Time.time);

if (currentAmmo == 0)
        {
            if (Input.GetKey(KeyCode.R))
            {
                nextReloadTime = Time.time + reloadTime;
                reload.Play();

                Debug.Log("Reload @ " + nextReloadTime);
            }
            else if (nextReloadTime >= Time.time)
            {
                Debug.Log("Reload");
            }
        }

```

I have it in the void Update () function.

Whenever I debug it out I get nextReloadTime output properly with the reloadTime variable added to the current time stamp but it outputs it’s reload at the time stamp exactly after it shows when it should reload at. Can somebody tell me why this is?

Thanks,  
James

---

<div class="post-metadata">

**Author:** ![Stardog](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/stardog/32/910746_2.png) [@Stardog](https://discussions.unity.com/u/Stardog)\
**Post date:** [November 1, 2015, 10:25am UTC](https://discussions.unity.com/t/reload-gun/603676/2 "2015-11-01T10:25:26Z")

</div>

Try this:

```csharp
public bool canReload;

void Update()
{
    if (Input.GetKeyDown(KeyCode.R))
    {
        if (!canReload) return;

        reload.Play();
        StartCoroutine(Cooldown(2));
    }
}

IEnumerator Cooldown(float timeToWait)
{
    // Cooling down
    canReload = false;

    yield return new WaitForSeconds(timeToWait);

    // Finished cooldown
    canReload = true;
}

```

---

<div class="post-metadata">

**Author:** ![LongformStudios](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/longformstudios/32/157583_2.png) [@LongformStudios](https://discussions.unity.com/u/LongformStudios)\
**Post date:** [November 4, 2015, 3:59am UTC](https://discussions.unity.com/t/reload-gun/603676/3 "2015-11-04T03:59:13Z")

</div>

> [@Stardog](#):
>
> Try this:
> 
> ```csharp
> public bool canReload;
> 
> void Update()
> {
> if (Input.GetKeyDown(KeyCode.R))
> {
> if (!canReload) return;
> 
> reload.Play();
> StartCoroutine(Cooldown(2));
> }
> }
> 
> IEnumerator Cooldown(float timeToWait)
> {
> // Cooling down
> canReload = false;
> 
> yield return new WaitForSeconds(timeToWait);
> 
> // Finished cooldown
> canReload = true;
> }
> 
> ```

Worked perfectly. Thank you!
