UE5 Variable is optimized away and not available when debugging

In this tutorial, we cover a debugging scenario where you will get the optimized variable message in the visual studio debugger when trying to watch variables. This hinders the debugging process as the developer can not see the variables he needs to troubleshoot.

Temporarily Turn Optimizations Off

Code optimizations are great because they speed up your code but the trade-off is less debugging information. To disable code optimizations for unreal engine code you need to turn them off at the module level. We do this in the build.cs

 

Go to your module’s build.cs and add this line:

 

OptimizeCode = CodeOptimization.Never;

 

This will disable code optimization for that module and you should be able to see all variables when debugging code in that module. Remember to comment out or remove the line when publishing or when done debugging.