So I have a method, lets say MethodA (). I am calling this method from multiple places and somewhere along the line something breaks and MethodA is being called incorrectly.
Can I put a debug.Log line anywhere in MethodA that will tell me “MethodA was called from [class/method]”?
So basically I just want it to print out to me where it’s being called from so I can figure out where it’s breaking.
Just place your Debug.Log() inside your MethodA. When you click the log entry in the console you can see the stacktrace in the bottom detail view of the console. The stacktrace is in reverse order. So the point of your Debug.Log is on top. Followed by the method that called your method. Followed by the method that called the method which called your method (you get the idea ^^). Each stacktrace item also shows the line number within the source file at the end.
Disclaimer: I have never attempted this in Unity code so there is a small chance it won’t help, but it works in regular C#
You can get the caller from stacktrace:
StackTrace stackTrace = new StackTrace();
MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
Find out the specific method that called it:
string typeName = methodBase.DeclaringType.Name;
string methodName = methodBase.Name;
I hope that was helpful!