Last active
August 29, 2015 14:03
-
-
Save nastajus/e420713c2aa3be04b8cb to your computer and use it in GitHub Desktop.
Had difficulties implementing Coroutines per this site http://unitypatterns.com/introduction-to-coroutines/ I can't figure out the cause of the problem. I've tried restarting Unity and rebooting my PC. I've tried passing string and method names. Nothing ever changes. I'm always consistently getting either 1 Hello when yield comes before Debug.Lo…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class CoroutineCounter : MonoBehaviour { | |
// Use this for initialization | |
void Start () { | |
//StartCoroutine("CountSeconds"); //works | |
StartCoroutine("SayHelloEveryFrame"); //doesn't work | |
//StartCoroutine("SpawnShpere5Times"); //works | |
//StartCoroutine("SayHello5Times"); //doesn't work | |
//StartCoroutine("Countdown"); //works | |
//StartCoroutine(CountSeconds()); //works | |
//StartCoroutine(SayHelloEveryFrame()); //doesn't work | |
//StartCoroutine(SpawnShpere5Times()); //works | |
//StartCoroutine(SayHello5Times()); //doesn't work | |
//StartCoroutine(Countdown()); //works | |
} | |
//I THOUGHT MAKING MOVE MIGHT HAVE A SIDE-EFFECT OF CORRECTING PROBLEMS WHERE COROUTINES AREN'T WORKING CORRECTLY. | |
void Update(){ | |
transform.position += new Vector3(0, 0, .1f); | |
} | |
IEnumerator Countdown(){ | |
for (float timer = 3; timer >=0; timer -= Time.deltaTime) | |
yield return 0; | |
Debug.Log ( "This message appears after 3 seconds!" ); | |
} | |
//DOES WORK. | |
IEnumerator SayHelloFiveTimes(){ | |
yield return 0; | |
Debug.Log ("Hello"); | |
yield return 0; | |
Debug.Log ("Hello"); | |
yield return 0; | |
Debug.Log ("Hello"); | |
yield return 0; | |
Debug.Log ("Hello"); | |
yield return 0; | |
Debug.Log ("Hello"); | |
} | |
//DOESN'T WORK. | |
IEnumerator SayHello5Times(){ | |
for (int i = 0; i < 5; i++){ | |
//instructions: comment out other block | |
yield return 0; | |
Debug.Log("Hello"); //prints once | |
//Debug.Log("Hello " + i); //prints 5 times | |
// Debug.Log("Hello"); //prints twice | |
// yield return 0; | |
} | |
} | |
//DOES WORK. | |
IEnumerator SpawnShpere5Times(){ | |
for (int i = 0; i < 5; i++){ | |
yield return 0; | |
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); //creates 5 times | |
go.transform.position = new Vector3(0,0,0); | |
} | |
} | |
//DOESN'T WORK. | |
IEnumerator SayHelloEveryFrame(){ | |
while(true){ | |
//1. Say "hello" | |
Debug.Log("Hello."); //prints twice | |
//2.Wait until next frame | |
yield return 0; | |
}// 3. This is a forever -loop, go to 1. | |
} | |
//DOES WORK. | |
IEnumerator CountSeconds() | |
{ | |
int seconds = 0; | |
while (true) | |
{ | |
for (float timer = 0; timer < 1; timer += Time.deltaTime) | |
yield return 0; | |
seconds++; | |
Debug.Log(seconds + " seconds have passed since the Coroutine started."); | |
if (seconds == 5) StopCoroutine("CountSeconds"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment