Search Unity

Knowing How To Know What Function Called Function Help

Discussion in 'Scripting' started by renman3000, Jul 26, 2013.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Is there some sort of elite, special ops way to know what function called a function?

    Meaning, I have a funciton being mysteriously called. Is there some super top secret technique to know what function is calling this function besides placing prints all over the place?


    Optimism on this is low.
     
  2. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Thanks Karl,
    Code (csharp):
    1.  
    2. // get calling method name
    3. Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
    4.  
    The above line, where would it need to go? In each function? And would this overall be a whole script onto itself?
     
    Mikhail94 likes this.
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    I'd assume that
    Code (csharp):
    1. stackTrace.GetFrame(1).GetMethod().Name
    is the function that called the current function
    Code (csharp):
    1. stackTrace.GetFrame(0).GetMethod().Name
    is the current function


    Discussed here:
    http://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method

    They say it might not work outside of debugging.

    Another way suggested from that thread that you might want to try:
    Code (csharp):
    1. public string SendError(string Message, [CallerMemberName] string callerName = "")
    2. {
    3.     Console.WriteLine(callerName + "called me.");
    4. }
     
    Last edited: Jul 26, 2013
    UtopianX8 likes this.
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If you want a quick a dirty way and you're in the Editor then just throw an Exception in that method. It'll get logged, along with a stack trace, to the console.
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Cheers, got it working thanks.


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Diagnostics;
    5.  
    6.  
    7. public class someScript : MonoBehaviour {
    8.  
    9.     StackTrace stackTrace;
    10.  
    11.     void someFunctionThatINeedToKnowWhoCalledIt() {
    12.         stackTrace = new StackTrace();
    13.         print("stackTrace !! " + stackTrace.GetFrame(1).GetMethod().Name);
    14. }
    15. }
    16.  

    This will show the originating member.
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hi, I have come back to this code, as above.. to trace where a method is being called from. However, in the current case, with identical code, the print to console shows only the function that it exists in.

    So...

    Code (csharp):
    1.  
    2. public void closeBubble(){
    3. StackTrace stackTrace = new StackTrace();
    4. print("stackTrace !! " + stackTrace.GetFrame(0).GetMethod().Name);
    5. }
    ..only shows the closeBubble function. There is no trace of where this is being called from.


    Any ideas?
    Thanks
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Like I said 2 years ago. Throw an exception.

    Code (csharp):
    1.  
    2. public void closeBubble()
    3. {
    4.     throw new Exception("Dirty exception");
    5. }
    6.  
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Or just Debug.Log. The stacktrace shows up in the console window when you click on the log.
     
    KelsoMRK likes this.
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Of course if you're looking for an IDE solution, right clicking the method and choosing Find References will give you a list of every place in your code that method is called.
     
    MathewHI, yotingo and Kiwasi like this.
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I prefer Debug.LogError with error pause turned on. It will freeze the game state when the method is called, as well as printing a stack trace to the console.

    This works too, but it won't find places where you call a method via reflection. So SendMessage, pure reflection, or UnityEvents in the inspector.
     
  12. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    What are you trying to achieve?

    ie: Exception occurs and you're not sure why?

    If you are using Visual Studio and have the debugging setup, you can choose to view exceptions as they are thrown. Not sure what you're trying to do though, so that might not be useful
     
  13. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Thanks guys.