Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question UnityEngine.SetupCoroutine.InvokeMoveNext

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

  1. Drake4

    Drake4

    Joined:
    Jun 17, 2022
    Posts:
    30
    Guys,

    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?

    Cheers!
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    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