I am making a reload time, but when I start a coroutine, it doesn’t wait for 3 seconds and then reloads. Here is my code:
void Update(){
if(ammoInClip == 0){
StartCoroutine("reload", 3f);
ammoInClip = 30;
}
}
IEnumerator reload(float wait){
yield return new WaitForSeconds (wait);
}
The problem happens because when you yield
, you immediately return the control to the Update
method. It then executes the ammoInClip = 30
. After 3 seconds, the control returns to Reload
- that does nothing.
That’s why you have to put ammoInClip = 30
after the yield, so it can return the control to the Update
method, continue the game and come back after 3 seconds to reload.
void Update(){
if(ammoInClip == 0){
StartCoroutine(Reload(3f));
}
}
IEnumerator Reload(float wait){
yield return new WaitForSeconds (wait);
ammoInClip = 30;
}
Note the call for StartCoroutine
, I’m not using a string, better for type-safety.
Do you come from Java? Methods in C# usually starts with lowercase 
With the use of code and whenever you want to StartCoroutine that has require the method name and its argument if you have passed.
void Update(){
if(ammoInClip == 0){
StartCoroutine(reload(3.0f));
ammoInClip = 30;
}
}
IEnumerator reload(float wait){
yield return new WaitForSeconds (wait);
}