C# Noob Confused About IEnumerator

Hello All… I’m messing around with a Third person Shooter, Atm the character reloads Instantly so its very unrealistic so i created a float

public float reloadTime;

then inside my reload function i added

yield return new WaitForSeconds(reloadTime);

But this causes issues so i changed Void Reload() to IEnumerator Reload() The Error goes away but now the reload function isn’t being called.

This is the reload Function “NON edited”

public void Reload() {
	Debug.Log("Reload [" + weaponsSet[weaponIndex].weaponName+"]");
	int needBullets = (weaponsSet[weaponIndex].clipSize - weaponsSet[weaponIndex].currentBulletsCount);
		
	if (weaponsSet[weaponIndex].haveBullets >= needBullets){
		weaponsSet[weaponIndex].haveBullets -= needBullets;
		weaponsSet[weaponIndex].currentBulletsCount = weaponsSet[weaponIndex].clipSize;
	}
	else{
		weaponsSet[weaponIndex].currentBulletsCount = weaponsSet[weaponIndex].haveBullets;
		weaponsSet[weaponIndex].haveBullets = 0;
	}
}

I really only the basics of Javascript so C# is really confusing me and i don’t understand what im doing wrong.

1 Answer

1

in order to call a coroutine you need to send it through StartCoroutine like so:

StartCoroutine(Reload());

Here is a link to the reference page - http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html

Cheers! This works.

–

Dang got to it before me lol, good answer +1 :)

–

Unity Gems has a much better [article on coroutines][1], for C# (and not only) "confused noobs" :) [1]: http://unitygems.com/coroutines/

–

@ArkaneX good ref, +1

–

@ArkaneX Thanks for the link to Unity Gems, I have quite enjoyed the past 2 hours reading and watching videos there :)

–