Search Unity

Null Reference in Coroutine only accessing floats and method calls

Discussion in 'Scripting' started by zdlogan, Aug 22, 2014.

  1. zdlogan

    zdlogan

    Joined:
    Nov 5, 2013
    Posts:
    6
    We have received 4 crash reports for our mobile game from hockeyapp with the following null reference error in a coroutine:

    NullReferenceException: Object reference not set to an instance of an object
    at RenderQueueOverride+<crUpdateQueue>c__IteratorC5.MoveNext ()

    The coroutine in question is started in OnEnable and only makes method calls, starts other coroutines, or accesses a float parameter so I'm really not sure what could be causing the null reference. Here is the coroutine in question:

    Code (CSharp):
    1.     float m_UpdateInterval = 1f;
    2.  
    3.     IEnumerator crUpdateQueue()
    4.     {
    5.         yield return null;
    6.         yield return null;
    7.  
    8.         UpdateRenderQueue();
    9.         yield return StartCoroutine(crWaitRealtimeSeconds(0.1f));
    10.  
    11.         for (;;)
    12.         {
    13.             UpdateRenderQueue();
    14.             yield return StartCoroutine(crWaitRealtimeSeconds(m_UpdateInterval));
    15.         }
    16.     }
    Any ideas what could be causing this exception?
     
    Last edited: Aug 22, 2014
  2. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Perhaps you can get that error also if something goes wrong in crWaitRealtimeSeconds (returning a null?)
     
  3. zdlogan

    zdlogan

    Joined:
    Nov 5, 2013
    Posts:
    6
    Here's crWaitRealtimeSeconds but I don't see anything obvious there either. Can the unity Time class be null?

    Code (CSharp):
    1.     IEnumerator crWaitRealtimeSeconds(float waitTime)
    2.     {
    3.         float startTime = Time.realtimeSinceStartup;
    4.         float endTime = startTime + waitTime;
    5.         while (Time.realtimeSinceStartup < endTime)
    6.         {
    7.             yield return null;
    8.         }
    9.     }
     
  4. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Since realtimeSinceStartup don't stop when you pause the game, it could happen that after a pause the coroutine ends before the expected time, so calling UpdateRenderQueue() again possibly too soon. I don't know if it's possible that UpdateRenderQueue() could be called again before it ends and what are the implications of that...