Starting some long running operation, passing a callback delegate into it.
When the operation completes, the delegate gets called.
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).
check the scripting API, StartCoroutine function takes a string as parameter for methodname… i couldn’t really say why it would throw a nullref ex, because i can’t see the code behind StartCoroutine
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).
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?
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.
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?
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.
Even if there was a null reference in the coroutine, the crash comes from StartCoroutine itself (e.g: my code doesn’t run yet).
using UnityEngine;
using System.Collections;
public class StartCoro : MonoBehaviour {
// Use this for initialization
void Start () {
StartCoroutine(Test());
}
// Update is called once per frame
void Update () {
}
IEnumerator Test()
{
return null;
}
}
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?
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.
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.