Hi, I have two different scripts and one of them has a function. Is there a way to check if that function has been called in the other script? Any help would be GREATLY appreciated.
You can log a message when the function is called using: Debug.Log("Function called!");
You can store a bool that starts as false and set it to true when you enter the function. You can then check this bool elsewhere in code to tell whether your function has been called. Also if the script that holds the function is a MonoBehaviour AND the bool you created is either public or serialized then you will see whether or not that function has been called in the script’s inspector.
public bool functionHasBeenCalled = false;
// alternatively: [SerializedField] private bool functionHasBeenCalled = false;
public void DoTheThing()
{
functionHasBeenCalled = true;
...
}
If you’re asking because you’re debugging then you can attach your IDE (e.g. visual studio) to unity and add a break point in the function, this will stop the code executing meaning you can see the stack trace (all the functions that have been called to get to that point in the program) and look at what other variables’ values are.
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.