actually i’m coding a Traffic-light-System. and now i stuck at the point of changing from red to green. How can i wait in the code before it continues, but dont stop the whole Game.
actually i have this, but this stops the whole game
trafficLight.ChangeStatus(Status.Yellow);
System.Threading.Thread.Sleep(2000);
trafficLight.ChangeStatus(Status.Green);
i need to light up the yellow light for about 2 seconds, then it should turn on the Green light, how to do this?
Quick example in C#:
public class TrafficLight : MonoBehaviour {
void Start () {
StartCoroutine (GreenYellowRed ());
}
IEnumerator GreenYellowRed()
{
while (true) {
Debug.Log ("Green");
yield return new WaitForSeconds(3.0f);
Debug.Log ("Yellow");
yield return new WaitForSeconds(1.0f);
Debug.Log ("Red");
yield return new WaitForSeconds(3.0f);
}
}
}
im pretty useless scripter but i needed the same thing and so i did it with calculateing stuff and it does not need some kind of really complicated stuff i think this may help you:
public int one;
public int two = 50;
if (one <= two){
one = one -1;
if (one == 40){
dosomethingfirst
}
if (one == 30){
dosomethingsecond
}
}
if (one == 0){
one = two;
}
}
So far so good. What if I need to wait for some period of time from a class that have to be instantiated and not inheriting from MonoBehaviour ? How can I do that ? Thread sleep is not acceptable, also I don’t have acces to coroutines. And by the way I also want to be able to wait in the Utility class where every method is static and have to be accessed from anywhere.