- Home /
Question by
JotaRata · Mar 16, 2015 at 12:20 AM ·
programmingfunctionmethodvoid
Get the Caller of a static function?
Well, my problem is simple:
If i have a Static method, but i want to get the object which is calling that method something like:
public class MyClass {
public static void PrintMyName(){
string str = "Hi, my name is: ";
Debug.Log(str+ caller.gameobject.name +"!");
}
}
And in a Object B call that function:
//Object B with name AsDF
gameobject.name="AsDF";
//call function
MyClass.PrintMyName();
this should print in the console:
Hi, my name is AsDF!
but the question is how to get the GameObject which is calling that function?
Thanks for reading!
Comment
You may have the wrong design, if a static method should know the caller then maybe it should be made non-static.
Answer by Eluate · Mar 16, 2015 at 01:00 AM
You would need to put the GameObject as a paremeter of the static method.
eg.
public static void PrintMyName(GameObject objectThatCalledThisMethod)
Then you would do:
Debug.Log(objectThatCalledThisMethod.name);