1
\$\begingroup\$

Is there a way to instruct Unity compiler not optimize (best only for builds run from Unity editor)? More precisely, I would like it to stop removing local variables, because it complicates debugging. For example, inspecting node

void Generate() {
   var node = new Node();
   node = node; //breakpoint here, node cannot be inspected
   var x_nearest = Nearest(new_node);
   Debug.Log(new_node);//new_node still cannot be inspected, but x_nearest can
}
Node Nearest(Node node)
{
   //node is an argument here and can be inspected
}

in Visual Studio produces error rather than displaying its value.

The identifier node is not in the scope

A solution would be make every local variable a private member instead:

private Node node;
void Generate() {
   node = new Node();
   node = node; //breakpoint here, node can be inspected
}

this would, however require frequent back and forth refactoring. I am using Unity 5.6.0f3 with C# 6.0 support and Visual Studio 2017.
note: provided code is for illustration only and the question is not about debugging said code because reproducing specific compiler behavior is not trivial

\$\endgroup\$
  • \$\begingroup\$ I can't say for sure, but it sounds like it might be an issue with Visual Studio debugger with Unity. I frequently use the debugger in Monodevelop and I can inspect local variables at breakpoints without a problem. \$\endgroup\$ – Tartle Wizard May 3 '17 at 17:01
  • 1
    \$\begingroup\$ I think its the fact that node = node is a useless line (it does nothing) so it gets removed. Then var node = new Node() does nothing because the object is never used, so that gets compiled out. Do Debug.Log(node) instead of node = node and try that. \$\endgroup\$ – Draco18s May 3 '17 at 17:06
  • \$\begingroup\$ @Draco18s Debug.Log had not effect - the node is not unused, there is more logic in the function - if I step inside a function var x_nearest = Nearest(new_node); the new_node is within said function argument and is inspectable. This might be bug in VS as @Tartle Wizard said, but if it is inspectable when passed as argument it really looks like non-existent variable in time of execution. Maybe I should change the title, but I am not sure what would better describe the problem? \$\endgroup\$ – wondra May 3 '17 at 17:17
  • \$\begingroup\$ Try setting the variable as volatile. That should force the compiler to disable any optimization related to that variable. So, a line like this: volatile Node node = new Node(); should do what you need it to do. (Also, try not to use var to declare variables. It's just good practice to actually call it what it is.) \$\endgroup\$ – John Hamilton May 4 '17 at 5:09
  • \$\begingroup\$ @JohnHamilton Local variables cannot be declared volatile. For thevar keyword the "official" rule is that everything except primitive types should be declared var, unless it impairs the readability. \$\endgroup\$ – wondra May 4 '17 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.