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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question UnityEngine.SetupCoroutine.InvokeMoveNext

Discussion in 'Scripting' started by Drake4, Mar 26, 2023.

  1. Drake4


    Jun 17, 2022

    I am getting this error:

    NullReferenceException: Object reference not set to an instance of an object
    [reference to a line of code which does a debug print]
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <24d45e813e524a99bfb7a145158a7980>:0)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr, Boolean&)

    Typically I would suspect the the debug line uses some variable which is null. But in this case the reference to the IEnumerator throws me off. Is it possible that if you call the same coroutine for different objects mutlible times (e. g. having a script which changes color of the object ever 5 seconds and you run this at 5 different objects). Would you need to call the coroutine in a special way?

  2. chemicalcrux


    Mar 16, 2017
    No, you don't need to do anything special. Something in that line of code is dereferencing a null reference.

    If you're curious what that function name means, coroutines are just enumerators. An enumerator tells you its current value and has a function you can call to get the next value.

    So, a list iterator tells you the thing at index 0, then, after calling MoveNext(), tells you the thing at index 1. So on and so forth until you hit the end.

    Every time you run a statement with "yield" in front, your coroutine finishes a MoveNext() call and yields a value -- maybe null, maybe an instance of WaitForSeconds, etc. etc.

    So, if your coroutine throws an exception, you'll probably see InvokeMoveNext in the stack trace, since that's the function where Unity calls MoveNext() on your coroutine.
    Last edited: Mar 26, 2023