- Home /
Is there any way to check which method called a certain other method?
Question says it all. I currently have a set of methods that call other methods. However these other methods are only very slightly different. If I can determine which method called a certain method I could condense all these other methods into one with an if-else structure.
If it is just for identifying what needs to be changed and not to be used in runtime, you could get the traceback. In fact, if you just add a Debug.Log() and expand your console a little, it shows where it came from, which may be useful!
Answer by kbumpious · Sep 04, 2015 at 11:28 PM
Pass a string from one function to the other.
void func1 (){
func3("itwasfunc1");
}
void func2 (){
func3("itwasfunc2");
}
void func3 (string thefuncthatcalledme){
Debug.Log(thefuncthatcalledme);
}
How would I go about it if I used Invoke to call the other method?
void LaunchShipA()
{
Invoke ("ALaunch", (Random.Range(0, 10)));
}
void LaunchShipB()
{
Invoke ("ALaunch", (Random.Range(0, 10)));
}
void ALaunch()
{
Rigidbody clone;
clone = Instantiate(enemyShip, spawnPointArray[0], transform.rotation) as Rigidbody;
}
I am not sure on the neatest way to do it in that case. What I would do is just create a public variable and change the value of that in your functions and then log it from your ALaunch function:
public string whoLaunched;
void LaunchShipA()
{
Invoke ("ALaunch", (Random.Range(0, 10)));
whoLaunched = "ShipA";
}
void LaunchShipB()
{
Invoke ("ALaunch", (Random.Range(0, 10)));
whoLaunched = "ShipB";
}
void ALaunch()
{
Debug.Log(whoLaunched);
Rigidbody clone;
clone = Instantiate(enemyShip, spawnPointArray[0], transform.rotation) as Rigidbody;
}
Your answer
Follow this Question
Related Questions
How can I call a method from another script? 3 Answers
How to check if a function is called? 2 Answers
Calling Function in other Script via Touch => iOS Crash 1 Answer
Calling A Function Once 0 Answers
Calling a function only once in Update 3 Answers