I know that I could write a method within the singleton that requires, as a parameter, the name of the gameobject that’s triggering the singleton method. But I’m wondering if that data is already in there somewhere – something that the singleton method “just knows.” Any thoughts?
No, that “data” isn’t available. The caller actualy doesn’t have to be an object. The method inside the singleton could be called from a static class / method in which case there is no object.
There are the StackTrace and StackFrame classes in .NET / Mono, but even those can’t tell you which object has called a certain method, only which method has called the method. From the method info you could determine which class that method belongs to, but not which object. That information isn’t necessary to call a method and as mentioned above sometimes even not available (in case of a static method).
If the method need to know about the object that called the method, you should pass that object as parameter to the method. Don’t just pass the object name, pass the object reference itself.
// inside your singleton:
public void SomeMethod(GameObject aCaller)
{
Debug.Log("SomeMethod got called from gameobject: " + aCaller.name + "which is at position: " + aCaller.transform.position);
}
While you could this from Reflection:
StackTrace stackTrace = new StackTrace();
MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
Console.WriteLine(methodBase.Name);
A much simpler and more efficient would be to pass the script itself to the singleton’s method call:
yourSingleton.SomeMethod(this);
This is a bit cleaner than the string and more type safe.