Is there a way to "Dubug.Log" which gameobject called a certain function?
LEt´s say I have 3 objects and all three of them can call a certain public function in a 4th script. Is there a way to "Dubug.Log" which gameobject called that function?
Can you add a GameObject parameter to the function on script 4? You could just pass the GameObject that calls the function and use that for the debug.log
void ExampleScript(int filler, float moreFiller, GameObject callingObj)
{
//Do Stuff
Debug.Log("Called by: " + callingObj.name);
}
Answer by ItzChris92 · May 19, 2017 at 07:33 PM
You could put a Debug.Log("Object 1 called method") etc. after each line in which your method is called. Not the best but it's simple enough
Answer by patosalas · May 20, 2017 at 04:31 AM
Thank you for your time, actually I can´t do that. The three objects call that method if a boolean is on, so that way it could tell me who called it, but it could be called many times. For reasons too complex to explain here I need to "debug.log" it from script 4. Thanks anyway!
Have you tried to put the debug on script 4, and click on it in the console? It should come up with a path of sorts (the code execution order) and maybe you can deter$$anonymous$$e from there
I´m not sure what you mean. I did put it in script 4, but it only shows how many times the method gets called. What do you mean by "click it on the console"?
Answer by thundermircea · Apr 06, 2021 at 07:32 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) inside the function you want to track and you'll get an error in the console detailling the history of all function calls that eventually triggered your function.