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?