- Home /
Question by
connorwforman · Nov 20, 2017 at 11:22 PM ·
scripting problemaudiosourceclippingreloading
Best way for sound and reload?
I have been having this REALLY annoying problem with my reload scripts. Have tried 2 different ways to do it, and they both work. Either the audio clips (repeats at the beginning), or ReloadTime does not wait enough before reloading. Please, if there are any spelling mistakes, do not comment on it, I will figure it out eventually.
Attempt 1 (audioClipping):
public float ReloadTime = 3.0f;
public float CurrentAmmo;
public float MaxAmmo = 31f;
public Audiosource ReloadSound;
void Update () {
if (Input.GetButtonDown("Reload Button")) {
StartCoroutine Reload());
}
}
IENumerator Reload () {
yield return new WaitForSeconds (ReloadTime);
ReloadSound.Play();
CurrentAmmo = MaxAmmo;
}
Attempt 2 (doesn't wait enough):
public float ReloadTime = 3.0f;
public float CurrentAmmo;
public float MaxAmmo = 31f;
public Audiosource ReloadSound;
void Update () {
if (Input.GetButtonDown("Reload Button")) {
Reload();
}
}
void Reload () {
ReloadTime -= Time.deltaTime;
ReloadSound.Play();
CurrentAmmo = MaxAmmo;
}
Comment
Best Answer
Answer by voncarp · Nov 20, 2017 at 11:39 PM
Looks like you may need a boolean to make sure it is not calling your coroutine several times. Try adding Bool isReloading as such:
public float ReloadTime = 3.0f;
public float CurrentAmmo;
public float MaxAmmo = 31f;
public Audiosource ReloadSound;
bool isReloading;
void Update () {
if (Input.GetButtonDown("Reload Button") && !isReloading) {
isReloading = true;
StartCoroutine Reload());
}
}
IENumerator Reload () {
yield return new WaitForSeconds (ReloadTime);
ReloadSound.Play();
CurrentAmmo = MaxAmmo;
isReloading = false;
}
Answer by shadowpuppet · Nov 21, 2017 at 01:34 AM
voncarp has it I think. you are stuck in that coroutine