- Home /
How to track codes from unity console.
hi. i am trying to figure how to track codes in script from unity console.
here is what i want to achieve:
using Debug.Log(), i want to print this into unity console to track and monitor some codes specifically methods
1 all the codes in a particular line. because from what i understand codes are executed line by line. i will show example on what i want here.
2 last method that was called before Debug.Log(). Or if possible last 2 or 3 or more method before that
heres the examples of what i want;
// lets say line execution starts here
// i dont know exactly the order but i numbered them just for reference in this example. at least this s how i assumed
public void MainMethod() //lets say this method called form somewhere
{
method1(); --------------------------executed line1
method2(); --------------------------executed line5
}
public void method1()
{
//some stuff here --------------------------executed line2
//some other stuff here --------------------------executed line3
//more stuff here --------------------------executed line4
}
public void method2()/// i want to debug from this method
{
Debug.Log( the last method that was called which in this case method 1 ); --------------------------executed line6
Debug.Log( executed line3 ); // what i mean here 4 lines that was executed before reaching here
}
Hope i am clear enough with this example.
i have read some similar post and they suggested stacktrace.
i found information on stacktrace, but i dont really get how to use it and the examples given is really not clear, to me at least.
i tried to test it out or figuring out how to use it myself but dint work.
is there a proper way to achieve this.
and if this is all about stacktrace pls give some clear and simple examples on how to use it.
thank you in advance. cheers
are you using monodevelop? on this case you can just add a breakpoint on line 6 and after follow step by step to see the workflow.
@DCordoba hi.. thanks for the suggestion. i am using visual studio. i am aware of the breakpoint tool.. but it doesnt really achieve my goal. since i want the log to be displayed in unity console.
my case actually is, i have a class with a constructor. in the constructor i have lots of methods. one of these methods will be called depending on the parameter i passed (kinda work like a code for which method to call). i specifically want the exact position from where the constuctor i called from. hope this helps to clear up my goals.
You could use a wrapper function to deter$$anonymous$$e the origin, something like this.
private void _DoSomething(int someInt)
{
/this is the code that you actually want to run;
}
public void DoSomething(object source, int someInt)
{
Debug.Log("Called from " + source);
_DoSomething(someInt);
}
Since it's a constructor though, I'd do something like...
public class $$anonymous$$yObject
{
public static $$anonymous$$yObject Create(object source, int someInt)
{
Debug.Log("Called from " + source);
return new $$anonymous$$yObject(someInt);
}
public int someValue;
private $$anonymous$$yObject(int someInt)
{
myObject.someValue = someInt;
}
}
@RobAnthem Thanks for the suggestions. i did use the first method like you suggested. it works as it should. but not really what i wanted. but the second method kinda brings me a step closer to what im working at. i never thought of using return and create class at the same time. thanks anyway.
Well another, more indepth option, if you're less concerned about the actual object that calls the constructor and more about which section of code called it, you could use the StackFrame like this.
public class $$anonymous$$yObject
{
public static $$anonymous$$yObject Create(System.Diagnostics.StackFrame source, int someInt)
{
Debug.Log("File: " + source.GetFileName()
+ ", Line: " + source.GetFileLineNumber());
return new $$anonymous$$yObject(someInt);
}
public int someValue;
private $$anonymous$$yObject(int someInt)
{
myObject.someValue = someInt;
}
}
and for calling the constructor you would use
$$anonymous$$yObject = $$anonymous$$yObject.Create(new System.Diagnostics.StackFrame(0, true), someIntValue);