0

I am currently working on a project in which i assign a movement routine to a GameObject. In the mentioned routine i assign a few variables which are used in the routine loop. However, once i access any of these variables inside of a lambda expression, things start to get weird.

Interestingly, this only seems to happen inside of an IEnumerator method.

When debugging with VS 2017, the variable is no longer listed under the locals window, neither can it be inspected. What's more, when attempting to inspect the variable, Unity may crash. What's causing the crash, i'm not sure, it seems like the allocation of the memory for the variable is missing, causing reads and writes from somewhere else to that same memory region or something like that.

I managed to reproduce this behavior on a new Project with the following MonoBehaviour:

using System;
using System.Collections;
using UnityEngine;

public class Test : MonoBehaviour {

    void Start () {
        Foo2();
        StartCoroutine(Foo());
    }

    void Foo2() {
        object bar = null;
        Action foobar = () => bar.ToString();
        // when placing a breakpoint on the above line, no problem will occur
    }

    IEnumerator Foo() {

        object bar = null;
        Action foobar = () => bar.ToString();
        // when placing a breakpoint on the above line, 'bar' will glitch and may crash unity
        yield break;
    }
}

My Unity setup:

  • Version: 2017.3.1f1
  • Debugger: Visual Studio 2017
  • Scripting runtime: .Net 4.6

Does someone know why this is happening and how i can prevent this?

spfi
  • 103
  • 6
  • Isn't `bar` null? Why are you trying to convert null to string? – Programmer Apr 18 '18 at 12:00
  • I tried to make the example as generic as possible, i have to do something with it in the lambda, so i went with this. I know that this wouldn't work, however it is sufficient to test this behavior – spfi Apr 18 '18 at 12:01
  • Ok but do you get the glitch or crash if bar is actually initialized? – Programmer Apr 18 '18 at 12:06
  • Sorry couldn't answer just now. Yes, it's really independent of the actual value or even type. In the project i'm working on i have multiple Collider2D instances from which some get values instantiated while others do not. this happens on all of them. – spfi Apr 18 '18 at 12:32

0 Answers0