I have two lines of code that I would like to run at the same time for more efficiency. When I remove the WaitForSeconds and just play the sound, I get an error saying “not all paths return a value”.
How would I combine the WaitForSeconds and the Audiosource play lines?
IEnumerator Reload () {
yield return new WaitForSeconds (ReloadTime);
reloadSound.Play();
CurrentAmmo = MaxAmmo
}
So, basicaly, if you don’t need WaitForSeconds in this case, you don’t even need a Coroutine.
If you don’t want to alter your code somewhere else, you can modify the Reload IEnumerator like this :
IEnumerator Reload () {
reloadSound.Play();
CurrentAmmo = MaxAmmo;
yield return null;
}
I think all you need to do is switch the first two lines in the coroutine so that the sound plays, then it waits while the sound is playing, then adds the ammo after that length of time. You could also use the length of the AudioClip for more accurate reload speeds/less playing around with times to try to match the reload to the end of the clip.
IEnumerator Reload () {
reloadSound.Play();
yield return new WaitForSeconds (ReloadTime);
//Alternatively, you could do the following:
//yield return new WaitForSeconds(reloadSound.clip.length);
CurrentAmmo = MaxAmmo;
}