- Home /
Is there any way for a script to get its caller?
say from within a script, i call Myclass.Myfunction()
is there any way to know within myfunction, what script/object called it, without the caller having to pass itself as an argument?
Answer by glenneroo · Mar 30, 2020 at 01:25 PM
At the top of your class, add this import:
using System.Diagnostics;
Then in your function:
string callingFuncName = new StackFrame(1).GetMethod().Name;
Answer by Mmmpies · Dec 07, 2014 at 08:36 AM
You've answered this one yourself! You really need to pass the information from the function/script that's doing the calling.
Is there an issue with doing that?
If it's just too much typing consider adding methodBase to all scripts that call your function. You'll need to add...
using System.Reflection;
then in your function
MethodBase mBase = MethodBase.GetCurrentMethod();
then call
myFunction(mBase.Name);
I suppose you could do this in monodevelop with replace in files but be careful.
Answer by thundermircea · Apr 06, 2021 at 07:31 PM
There is a pretty simple workaround, all you have to do is create a gameobject variable and set it to null, the add a Debug.Log(yourVariable.name) and you'll get an error in the console detailling the history of all function calls that eventually triggered your function.