Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Coroutine not running on device, but runs in editor mode

Discussion in 'Scripting' started by nick_tw, Oct 16, 2020.

  1. nick_tw

    nick_tw

    Joined:
    Oct 24, 2018
    Posts:
    4
    Have an empty object with a Map Scene script:

    Code (CSharp):
    1. public class MapSceneController : MonoBehaviour
    2. {
    3.     public GameObject introClouds;
    4.     public int cloudsDelay;
    5.     void Start() {
    6.         StartCoroutine(ExecuteCloudsAfterTime(cloudsDelay));
    7.     }
    8.     IEnumerator ExecuteCloudsAfterTime(float time) {
    9.         yield return new WaitForSeconds(time);
    10.  
    11.         CloudMover c = introClouds.GetComponent<CloudMover>();
    12.         c.doIt();
    13.     }
    14. }
    Here's my CloudMover script:

    Code (CSharp):
    1. public struct TransformData {
    2.     public Vector3 position;
    3.     public Quaternion rotation;
    4.  
    5.     public Vector3 localPosition;
    6.     public Vector3 localScale;
    7.     public Quaternion localRotation;
    8.  
    9.     public Transform parent;
    10. }
    11.  
    12. public static class TransformUtils {
    13.  
    14.     public static TransformData Clone(this Transform transform) {
    15.         TransformData td = new TransformData();
    16.  
    17.         td.position = transform.position;
    18.         td.localPosition = transform.localPosition;
    19.  
    20.         td.rotation = transform.rotation;
    21.         td.localRotation = transform.localRotation;
    22.  
    23.         td.localScale = transform.localScale;
    24.  
    25.         td.parent = transform.parent;
    26.  
    27.         return td;
    28.     }
    29.  
    30. }
    31. public class CloudMover : MonoBehaviour
    32. {
    33.     public GameObject[] cloudsMovingRight;
    34.     public GameObject[] cloudsMovingLeft;
    35.     //public GameObject background;
    36.     public Material spriteAlpha;
    37.     public float seconds;
    38.     // Start is called before the first frame update
    39.     void Start()
    40.     {
    41.        
    42.     }
    43.  
    44.     // Update is called once per frame
    45.     void Update()
    46.     {
    47.        
    48.     }
    49.     public void doIt() {
    50.         _ = StartCoroutine(SmoothMove());
    51.     }
    52.     public IEnumerator SmoothMove() {
    53.         float t = 0f;
    54.  
    55.         TransformData[] startingTransformsLeft = new TransformData[cloudsMovingLeft.Length];
    56.         TransformData[] startingTransformsRight = new TransformData[cloudsMovingRight.Length];
    57.         for (int i = 0; i < cloudsMovingLeft.Length; i++) {
    58.             startingTransformsLeft[i] = cloudsMovingLeft[i].transform.Clone();
    59.         }
    60.         for (int i = 0; i < cloudsMovingRight.Length; i++) {
    61.             startingTransformsRight[i] = cloudsMovingRight[i].transform.Clone();
    62.         }
    63.        
    64.         while (t <= 1.0) {
    65.             t += Time.deltaTime / seconds;
    66.             for (int i = 0; i < cloudsMovingLeft.Length; i++) {
    67.                 GameObject cloud = cloudsMovingLeft[i];
    68.                 cloud.transform.localPosition = Vector3.Lerp(startingTransformsLeft[i].localPosition, new Vector3(-8, cloud.transform.localPosition.y, cloud.transform.localPosition.z), Mathf.SmoothStep(0f, 1f, t));
    69.                 cloud.transform.localScale = Vector3.Lerp(startingTransformsLeft[i].localScale, new Vector3(4.5f, 4.5f, 4.5f), Mathf.SmoothStep(0f, 1f, t));
    70.  
    71.                 Color c = cloud.GetComponent<SpriteRenderer>().color;
    72.  
    73.                 c.a = Mathf.Lerp(132, 0, t) / 255.0f;
    74.                 cloud.GetComponent<SpriteRenderer>().color = c;
    75.             }
    76.             for (int i = 0; i < cloudsMovingRight.Length; i++) {
    77.                 GameObject cloud = cloudsMovingRight[i];
    78.                 cloud.transform.localPosition = Vector3.Lerp(startingTransformsRight[i].localPosition, new Vector3(8, cloud.transform.localPosition.y, cloud.transform.localPosition.z), Mathf.SmoothStep(0f, 1f, t));
    79.                 cloud.transform.localScale = Vector3.Lerp(startingTransformsRight[i].localScale, new Vector3(3.5f, 3.5f, 3.5f), Mathf.SmoothStep(0f, 1f, t));
    80.  
    81.                 Color c = cloud.GetComponent<SpriteRenderer>().color;
    82.                 c.a = Mathf.Lerp(132, 0, t)/255.0f;
    83.                 cloud.GetComponent<SpriteRenderer>().color = c;
    84.             }
    85.            
    86.  
    87.             Color spriteColor = spriteAlpha.color;
    88.             spriteColor.a = Mathf.Lerp(0, 1, t);
    89.             spriteAlpha.color = spriteColor;
    90.             //background.GetComponent<CanvasGroup>().alpha = Mathf.Lerp(0, 1, t);
    91.  
    92.  
    93.             yield return null;
    94.         }
    95.        
    96.     }
    97. }

    The doIt() function does not get called when running a production build, but it runs automatically in the editor run mode. Why is this?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    How did you verify that it is not running? What are you expecting to happen that is not happening?
     
  3. nick_tw

    nick_tw

    Joined:
    Oct 24, 2018
    Posts:
    4
    I have since added Text to my UI and adjusted the code

    Code (CSharp):
    1. public void doIt() {
    2.         Text t = debugText.GetComponent<Text>();
    3.         t.text = "doIt() has been called";
    4.         _ = StartCoroutine(SmoothMove());
    5.     }
    Text changes when I click Run in editor, but not when I deploy to device.
     
  4. nick_tw

    nick_tw

    Joined:
    Oct 24, 2018
    Posts:
    4
    Nevermind, I don't know why, but increasing cloudsDelay from 1 to 3 seconds seems to have fixed it.