Hi all,
I need to pass a “ref” parameter to a IEnumerator method, but I sadly found out that it’s not possible to use “ref” as a parameter in a iterator…here’s my code below, which of course throws an error:
public class CountDownTimer : MonoBehaviour {
public static IEnumerator CountDown (string objName, string displayEndText, float timer, ref bool boolToPass) {
// Create a new GameObject for the countdown timer and child it to "HUD"
GameObject countDownGO = new GameObject(objName);
countDownGO.transform.parent = GameObject.Find("HUD").transform;
// Create the GUIText component which displays the countdown and attach it to the countDownGO gameObject created above
GUIText countText = countDownGO.AddComponent<GUIText>();
countText.text = timer.ToString();
countText.font = (Font)Resources.Load("Fonts/Arial Black", typeof(Font));
countText.material = countText.font.material;
countText.anchor = TextAnchor.MiddleCenter;
countText.alignment = TextAlignment.Center;
countText.transform.position = new Vector3 (0.5f,0.5f,0.0f);
countText.lineSpacing = 1;
countText.tabSize = 0;
yield return new WaitForSeconds (1.0f); // Wait 1 second before starting the timer
// While the timer is bigger then 1, decrease it every seconds and round the number to an int
while (timer > 1) {
timer -= Time.deltaTime;
countText.text = Mathf.Round(timer).ToString();
// When timer reaches 1, wait 0.5 seconds, then display end text and call callback function, wait 1 second then destroy the gameObject
if (timer <= 1) {
yield return new WaitForSeconds (0.5f);
countText.text = displayEndText;
boolToPass = true;
yield return new WaitForSeconds (1.0f);
Destroy(countDownGO);
break;
}
yield return null;
}
} // End of CountDown IEnumerator function
I have looked around and found a few solutions, but I'm having a hard time understanding them and I haven't been able to successfully apply them to my own code. Could anyone show me how to do this with my code above?
Thanks a lot!
Stephane