1

Is it possible to get a proper reference to object that called our static method?

I call a static method from many places in Unity project and I'd like to pass a gameObject to context in UnityEngine.Debug.Log(object message, Object context) for easier debugging with Unity console. I already tried with System.Diagnostics.StackTrace class and found nothing but strings which aren't useful.

edit: Here's a code similar to what i need to achieve:

public static class DebuggingHelper {
    public static void LogText() {
        var callingObject = SomeStaticReflectionClasss.INeedToGetItSomehowHere();
        if(callingObject is UnityEngine.Object)
            UnityEngine.Debug.Log("Good morning", callingObject as UnityEngine.Object)
    }
}
salveiro
  • 419
  • 1
  • 5
  • 13
  • It's possible that something like `Assembly.GetCallingAssembly();` will help, but it's hard to be sure without more detail. https://stackoverflow.com/questions/5478814/c-sharp-get-calling-methods-assembly – Robin Bennett Jul 22 '19 at 10:28
  • If it's a manageable number of callers (a few dozen?) it might be easiest to just change the signature temporarily and then work through all the compile errors passing the gameObject, rather than trying to fish it out of the call stack. There are a few old questions here that say it isn't possible (e.g. [this one from 2010](https://stackoverflow.com/q/2237374/243245)): you can only get the class names, which I guess is what you're getting. It's possible things have changed since then though. – Rup Jul 22 '19 at 10:29
  • I've edited the question to better show the problem. – salveiro Jul 22 '19 at 11:12
  • a [`UnityEngine.Object`](https://docs.unity3d.com/ScriptReference/Object.html) is everything that inherits e.g. from [`Component`](https://docs.unity3d.com/ScriptReference/Component.html), `MonoBehaviour` (which inherits from `Component`) or [`ScriptableObject`](https://docs.unity3d.com/ScriptReference/ScriptableObject.html). If your class is not of those types but rather a simple custom class you can't use this. – derHugo Jul 22 '19 at 13:06

1 Answers1

1

You could use an extension method on the component:

public static class ComponentX
{
    public static void Log(this Component component, string msg)
    {
        Debug.Log(msg + " called from: " + component);
    }
}

Call it from a component script:

public class Game : MonoBehaviour
{
    void Start()
    {
        this.Log("Hello");
    }
}

Result:

Hello called from: Game (Game)

Iggy
  • 4,767
  • 5
  • 23
  • 34
  • Cool idea but here you know the caller because you pass it with function argument. I would rather like to create a reflection tool that gets the caller without passing it explicitly. I'm realising now it might be impossible... – salveiro Jul 23 '19 at 10:33
  • 1
    @salveiro it's passed implicitly, notice the call `this.Log("Hello");` – Iggy Jul 23 '19 at 11:07
  • 1
    Yeah but extension methods are just syntactic sugar. I need generic reflection solution that will get the object of caller inside called method. That anyway sounds like a heavy cheat so I guess i will abandon the problem. – salveiro Jul 23 '19 at 14:34