0
\$\begingroup\$

So, here is what I am after. Is there a way to identify which were the last n lines of code that were executed in Unity 5 (with C#) at a given point of the game?

I know I can use Debug.Break() to set break points to help identifying problematic parts of the code. However, when the codes escalate and multiple functions interact, it is sometimes difficult to identify what came immediately before the break point.

Therefore, my question. Is there a way to retrieve the lines of code that were executed for instance before a break point?

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Errr do you know what a call stack is? \$\endgroup\$
    – Vaillancourt
    Commented Jan 26, 2016 at 16:02
  • \$\begingroup\$ @AlexandreVaillancourt Yes I know. However, since I can't run code directly in my VS installation )have to run it in Unity directly), the call stack of the VS remains empty. \$\endgroup\$ Commented Jan 26, 2016 at 16:13
  • \$\begingroup\$ Oh ok then, perhaps something useful will come out of this. \$\endgroup\$
    – Vaillancourt
    Commented Jan 26, 2016 at 16:15

1 Answer 1

1
\$\begingroup\$

You can use the property Environment.StackTrace to get the current call-stack, i.e. which method called which method which called which method which called this method.

Most debuggers also expose this information in a more comfortable way.

This usually gives you a pretty good picture of what chain of events lead to this line being executed. It won't tell you which path of an if/else was taken before jumping into another method, though.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks for your answer. So, I've trying Debug.Log(System.Environment.StackTrace) to print the stack to the console. However, it only gave the number of the line that made such call: at System.Environment.get_StackTrace() in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Environment.cs:line 227 at SpatialP+<InspectBVH>c__Iterator0.MoveNext() in C:\Documents\Unity Projects\New Project\Assets\SpatialP.cs:line 613 UnityEngine.Debug:Log(Object) <InspectBVH>c__Iterator0:MoveNext() (at Assets/SpatialP.cs:613) \$\endgroup\$ Commented Jan 26, 2016 at 16:26
  • 1
    \$\begingroup\$ @KimShutter What else would you have expected? \$\endgroup\$
    – Philipp
    Commented Jan 26, 2016 at 17:18
  • \$\begingroup\$ @KimShutter You can often infer which lines executed previously within each function listed, since unless you're using goto, control flow doesn't jump about randomly - in many cases you can just work backwards from the given line. If you have a complex sequence of conditional branches preceding the lines where your stack trace capture is triggered then you'll likely need to instrument those yourself or use a debugger to step through the execution. \$\endgroup\$
    – DMGregory
    Commented Jan 26, 2016 at 17:45
  • \$\begingroup\$ @DMGregory That's exactly my case and that's precisely what I was expecting: I mean, as I've stated, the reason why I asked for a way to retrieve the last N lines of code executed was, as I said, because I have intricate code functions that may call each other. So, just knowing where the break was called is not enough: and by the way, that alone is useless, because as I said, that is just a matter of including Debug.Break() in Unity's code. What I need is precisely to know the lines that were executed before that. \$\endgroup\$ Commented Jan 26, 2016 at 20:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .