- Home /
IEnumerator not working
Im using this code to create a game timer across the internet, however its not counting down because it cant get passed yield return null; and I done know why. Thanks for any help
using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking;
public class GameManager : NetworkBehaviour {
public static GameManager instance;
Text gameTimeText;
public matchSettings matchset;
public delegate void OnPlayerKilledCallback(string player, string source);
public OnPlayerKilledCallback onPlayerKilledCallback;
private void Awake()
{
if (instance != null)
{
Debug.LogError("There are two GameManagers in the scene");
}
else {
instance = this;
}
StartCoroutine(CmdendGame());
}
[SyncVar]
public int Maintime;
#region Game-Timing
public GameObject gameEndText;
GameObject Canvas;
public IEnumerator CmdendGame()
{
Debug.Log("90");
int time = Maintime;
Debug.Log("91");
yield return new WaitForSeconds(0.01f);
Debug.Log("sdasd");
if (Canvas == null)
{
Debug.Log(5);
Canvas = GameObject.Find("PlayerUI");
}
Debug.Log("90");
Comment
Answer by andrew-lukasik · Mar 31, 2018 at 05:41 PM
You need a way to loop your code there to make a timer, for example:
double timer = 0;
IEnumerator TimerRoutine ()
{
double tickTime = 0.1;
YieldInstruction tick = new WaitForSeconds( (float)tickTime );
while( true )
{
//yield:
yield return tick;
//append time:
timer += tickTime;
//display time:
Debug.Log(
string.Format( "Wait for it: {0}", (int)timer )
);
//stop conditions (otherwise this is infinite "while" loop):
if( timer > 10.0 )
{
break;
}
}
Debug.Log("Adventure Time!");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612155700im_/https://answers.unity.com/themes/thub/images/avi.jpg)