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

NullReferenceException in StartCoroutine

Discussion in 'Scripting' started by liortal, Sep 10, 2014.

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,557
    I am getting reports in our crash tracking system, that users seem to be getting a NullReferenceException in StartCoroutine.

    The code roughly looks like this:

    Code (CSharp):
    1. void Start()
    2. {
    3.     DownloadSomething("url", Callback);
    4. }
    5.  
    6. void Callback()
    7. {
    8.     StartCoroutine(OnCompletion());
    9. }
    10.  
    11. IEnumerator OnCompletion()
    12. {
    13.     // ... code here
    14. }
    Essentially the steps are:

    1. Starting some long running operation, passing a callback delegate into it.
    2. When the operation completes, the delegate gets called.
    3. The completion delegate starts a coroutine that should do something (e.g: load a level).
    During my testing, i could not reproduce this exception. What can cause StartCoroutine to throw this exception? (Note that the stack trace i am getting is in StartCoroutine itself, not in my method).
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    use
    Code (csharp):
    1. StartCoroutine("OnCompletion");
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,557
    Can you elaborate? it works 100% of the times on my machine. Why should i make that change ?
     
  4. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
  5. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,557
    The first overload of StartCoroutine takes an IEnumerator (also there in the link). That shouldn't matter (unless i am missing something, if so, please explain).
     
  6. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    ow, my bad... didn't read that (i always just use string, which sounds kinda stupid now...)
    in that case.. could you post the exact error you are getting?
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You are missing nothing. Indeed what you are using is far superior because you are on the safe side when it comes to renaming method names.
    Do you have any further details to share regarding the error message?
     
  8. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,557
    The only error i am getting is the exception:

    NullReferenceException in
    UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine)
    The code we use is exactly as i described. This also never happened locally here. I cannot figure out what may be the issue.
     
  9. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Sorry, that is just not enough information to help you out. There are far too many unknowns. May it happen that the game object or the component in which the coroutine is supposed to be executed gets destroyed? Is it executed in another game object that might be destroyed? E.g. someOtherMonoBehaviour.StartCoroutine (...). Is it possible that there is a null reference exception within your coroutine?
     
  10. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Does OnCompletion return anything? If not, or if null, then basically StartCoroutine(null) is invoked, which is not valid.
     
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Sorry to tell you that, but this is not correct.
     
  12. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,557
    Trying to answer what was said above:

    @Dantus

    1. The coroutine method is on the same MonoBehaviour (like the example i posted). The object / component is not destroyed (only when the next level is loaded, which is what the coroutine should be doing.
    2. Even if there was a null reference in the coroutine, the crash comes from StartCoroutine itself (e.g: my code doesn't run yet).

    @SunnySunshine

    The coroutine yields (it has to, otherwise it doesn't compile). Even yielding null is OK, and won't result in the IEnumerator being null.
     
  13. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class StartCoro : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         StartCoroutine(Test());
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15.  
    16.     IEnumerator Test()
    17.     {
    18.         return null;
    19.     }
    20. }
    21.  
     
  14. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    A method returning an IEnumerator doesn't have to yield to compile. But if you're yielding then I don't know what's wrong.
     
    Last edited: Sep 10, 2014
  15. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,557
    I thought that doesn't compile. In any case, i have yield statements in my coroutine, so this is not the case.
     
  16. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,557
    Keep going guys, maybe something will throw me a lead to the solution :)
     
  17. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Is DownloadSomething a coroutine somewhere else?
    I see you're using a delegate there as a callback, any chance this monobehaviour gets destroyed and the callback is called anyway?
     
  18. 6565

    6565

    Joined:
    Feb 17, 2013
    Posts:
    19
    You should never use "return" on functions that return IEnumerator in Unity - always "yield return" it.
    "yield return null" doesn't throw an exception for example.

    And Lior, can you please place the actual code that you're doing? Kind of hard to debug it this way.
     
  19. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Sorry, I wasn't precise. What I meant is that if you return nothing, it won't compile at all. But you are certainly right about return null.
     
  20. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    True. I knew I had produced a NullReferenceException by doing something like that, just not which one of my suggestions so I thought I'd mention all possibilities. But indeed not returning will not compile.
     
  21. LangDong

    LangDong

    Joined:
    May 13, 2016
    Posts:
    1
    I met the same error, my script is not bound to the gameObject。After binding is solved