B will call happyFunction in D.
Imagine there are a number of other game objects (E, F, G and K) which individually also have script components, which also from time to time call happyFunction in D.
Question … inside D, is there a way of knowing “who called us” ? Was it game object A, E, F, G or K ? (Further perhaps, which component?)
I am completely and totally aware of workarounds for this, programming philosophy involved, etc etc etc, thanks … does anyone know the answer to the question?
#Jahroy supplies the answer:
var tt = new System.Diagnostics.StackTrace();
print("courtesy Jahroy 2012 : " + tt.GetFrame(1).GetMethod().Name );
Awesome. Thank you very much Jahroy. And thanks to Bunny, here is the c#
StackTrace st = new StackTrace();
Usually when you design a function, you have to think about what information it needs. If this function have to know something about another GameObject, pass it as parameter.
function HappyFunction(aObject : GameObject)
// here you can access components, the name, the position, the tag, whatever of aObject
you would call the function like this:
If it can only be called from a certain script, attached to multiple objects, you can also pass the script instance directly
function HappyFunction(aCaller : MyScriptName)
and call it like that to pass the reference of the current script:
Note: the second method works only if it’s the same script type, or at least a derived class.
The question is how do you distinguish between those GameObjects? The only true difference is the instance-reference. The name, tag, layer of multiple GameObjects can be the same.
I was all excited to read your question (after the last debacle) since I've had to do this before.
Unfortunately you and Bunny beat me to the punch with the solution I used.
You can do it like this:
var theTrace = new System.Diagnostics.StackTrace();
I used a technique like this for logging in the past.
I had a heck of a time guessing at how to use the TextureImporter functions when I wrote my platform override wizard. Luckily that will be obsolete when Unity 3.5 comes out.