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

Problem when debug in a corrutine. Variables in inspector are null but they aren't

Discussion in 'Scripting' started by jma00049, Jul 14, 2021.

  1. jma00049

    jma00049

    Joined:
    Feb 21, 2019
    Posts:
    89
    Hello, I am using Unity 2020.3.7f1 with Visual Studio 2017. I need to debug a function (corrutine) because I am not getting the result I want but all the variables in that function are shown as null even if they aren't (I have checked variables by using Debug.Log).
    The function I am debugging is a corrutine and the data I am trying to debug is after a yield return so I think that VS is loosing the reference.



    Anyone know what is happening? Thanks!
     
  2. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    342
    Coroutines are very tricky. When you call coroutine method, all code that was inside will grabbed instantly and sent to engine where it will be converted to some kind of state machine.
    It's complex stuff, and that's why try catch don't work inside coroutines (at least they didn't if I'm not mistaken, that was said by official unity dev on some conference), this isn't usual function, this code your are debugging is cut to pieces between yield statements and executed somewhere in engine. Maybe VS just hidding arguments because of that or some other thing.
     
    jma00049 likes this.
  3. jma00049

    jma00049

    Joined:
    Feb 21, 2019
    Posts:
    89
    Oh, I didn't know corrutines work in that way. I will have to debug using Debug.Log so.
    Thank you!