How can I start a coroutine at the same time as executing a regular function?

53 views Asked by At

I made a Reload() function that checks the current mag and ammo of a weapon and then reloads it, and I also want play sounds while I reload and so I made an IEnumerator to match the sounds with my animation. But now when I call both of those the Reload() function executes then the audio plays, I want both to happen at the same time.

Here is my script in the Update function: (the ws.getWeapon is just to check what weapon the player has equiped to play the corresponding animation and audio)

if (CurrentMag <= 0 && CurrentAmmo > 0 || Input.GetKeyDown("r") && CurrentMag < MagCap && CurrentAmmo > 0)
        {
            if (ws.getWeapon() == 0)
            {
                StartCoroutine(ReloadAudio(0));
                animator.Play("PistolReload");
            }
            else if (ws.getWeapon() == 1)
            {
                animator.Play("AK12_Reload");
            }

            Reload();
        }

Here is my Reload function:

void Reload()
    {
        if(CurrentMag <= 0 && CurrentAmmo > 0)
        {
            CurrentMag = 0;
            MagCount.text = CurrentMag.ToString();
            ReloadTimer -= Time.deltaTime;
            if (ReloadTimer <= 0)
            {
                ReloadTimer = ReloadTime;
                if (CurrentAmmo <= MagCap)
                {
                    CurrentMag = CurrentAmmo;
                    CurrentAmmo = 0;
                    AmmoCount.text = CurrentAmmo.ToString();
                }
                else
                {
                    CurrentMag = MagCap;
                    CurrentAmmo -= (MagCap - OldMag);
                    AmmoCount.text = CurrentAmmo.ToString();
                }

            }
        }
    }

And here's my ReloadAudio IEnumerator:

IEnumerator ReloadAudio(int ID)
    {
        if (ID == 0)
        {
            FindObjectOfType<AudioManager>().Play("G18_MagOut");
            yield return new WaitForSeconds(0.47f);
            FindObjectOfType<AudioManager>().Play("G18_MagIn");
            yield return new WaitForSeconds(0.7f);
            FindObjectOfType<AudioManager>().Play("G18_CockBack");
        }
    }
0

There are 0 answers