duck

Demo of Coroutine not working in flash because of foreach

Feb 24th, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class FlashCRTest : MonoBehaviour {
  6.    
  7.     List<string> a = new List<string>(new string[] { "thing 1", "thing 2", "thing 3" });
  8.    
  9.     // Update is called once per frame
  10.     void Update () {
  11.    
  12.         if (Input.GetKeyDown (KeyCode.Alpha1))
  13.         {
  14.             StartCoroutine(CR1()); 
  15.         }
  16.        
  17.         if (Input.GetKeyDown (KeyCode.Alpha2))
  18.         {
  19.             StartCoroutine(CR2()); 
  20.         }
  21.        
  22.     }
  23.    
  24.    
  25.     IEnumerator CR1() {
  26.         // Does not work in flash
  27.        
  28.         float startTime = Time.time;
  29.         while (Time.time < startTime+1) {
  30.             transform.Translate(Vector3.up * Time.deltaTime);
  31.             yield return null;
  32.         }
  33.  
  34.        
  35.         foreach (string s in a)
  36.         {
  37.             Debug.Log (s);
  38.             yield return null;
  39.         }
  40.  
  41.        
  42.     }
  43.    
  44.    
  45.     IEnumerator CR2() {
  46.         // Works in flash
  47.        
  48.         float startTime = Time.time;
  49.         while (Time.time < startTime+1) {
  50.             transform.Translate(Vector3.up * Time.deltaTime);
  51.             yield return null;
  52.         }
  53.                
  54.         for (int n=0; n<a.Count; ++n)
  55.         {
  56.             string s = a[n];
  57.             Debug.Log (s);
  58.             yield return null;
  59.         }
  60.  
  61.     }
  62.    
  63.    
  64.    
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment