- Home /
Checking if a function has been called from another script
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.
Answer by jackmw94 · Nov 10, 2020 at 12:24 AM
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.
Thank you very much! And I see how this works, but I guess I wasn't clear enough ( I updated the question above ). I'm trying to see if a function has been called in script A, and then script B recognizes it was called. Is this possible?
That script B would have to contain a reference to script A then it can check the ‘functionHasBeenCalled’ flag as mentioned above (considering it’s public).
if (scriptAReference.FunctionHasBeenCalled)
{
// B recognised function has been called on A
}
This will work to tell you whether it’s been called at least once but if you want something to happen in script B each time the function in script A is called then consider having the reference the other way round, with A holding a reference to B so it can tell B (via calling a method on B) each time it’s function is called.
public class ScriptA
{
public void TheFunctionHappened()
{
// logic that happens when A detects that a function was called on B
}
}
public class ScriptB
{
[SerializeField] private ScriptA _scriptA;
public void FunctionThatGetsCalled()
{
_scriptA.TheFuctionHappened();
}
}
After writing that out I feel I might have misunderstood again..
I think the first option you just mention might work, but how do I reference ScriptA on scriptAReference.FunctionHasBeenCalled?
Great question, I've assumed that these scripts are monobehaviours; if that's right then you'll want to add a serialized field to hold your reference to script A:
public class ScriptB : $$anonymous$$onoBehaviour
{
[SerializeField] private ScriptA scriptAReference;
private void Update()
{
if (scriptAReference.FunctionHasBeenCalled)
{
// B has recognised A has been called
}
}
}
Then in the inspector for ScriptB you'll see a new field available for ScriptA. Drag and drop this in and then ScriptB will have its reference.
If one or other aren't monobehaviours then let me know!
Answer by thundermircea · Apr 06, 2021 at 07:32 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613015455im_/https://answers.unity.com/themes/thub/images/avi.jpg)