check if part of function is executed in another script

rather new to programming.

I’m making a 2d game where you control a hand and have to defend against insects. I would like the insects to be destroyed when the controls (gameobjectet) are on top of the insects and a given function from the script/class associated with the controls are called.

I need to check if the function in question (Called: Attack) is called from another script that is associated with the insects. I thought to do like demonstrated in this thread: How to check if a function is called? - Unity Answers

but the function ‘Attack’ has a few ‘if statements’ within and I only want the insects to be destroyed if one specific if statement is carried through.

How do I check if part of a function in another script is executed?

public class EventClass:MonoBehaviour{

    public event Action OnClick = ()=>{};
    public void Attack (){
        if (Input.GetKeyDown (KeyCode.F)) {    
           chkinput = true;
        }
        if (chkinput) {
           timeframe += 1 * Time.deltaTime; 
           if (Input.GetKeyDown (KeyCode.G)) {
             print ("Input SucessFG");
             OnClick();
             timeframe = 0;
             chkinput = false;
           }
           if (timeframe > 1) {
             print ("Time Out");
             timeframe = 0;
             chkinput = false;
           }
       }
   }
}

public class BugMove:MonoBehaviour{
    private EventClass eventClass = null;
    void Start(){
         eventClass = GetComponent<EventClass>(); // If on another object, use GameObject.Find in front
         if(eventClass != null){
             eventClass.OnClick = this.OnClickDetected;
         }
    }
    void OnDestroy(){
         if(eventClass != null){
              eventClass.OnClick -= this.OnClickDetected;
              eventClass = null;
         }
    }
    private void OnClickDetected(){ Debug.Log("Gotcha!");}

}

So the first class has the checks and calls OnClick(). At that point, the EventClass does not care if it has any listener, it just calls the method, it could be there is no one listening.

BugMove is the listener, and it is done in the Start. It finds the component and register to the event with :

eventClass.OnClick = this.OnClickDetected;

Now, any time OnClick is called, it will also called OnClickDetected. OnDestroy removes the listener but it can be done anywhere.

Think of it like a following on twitter. EventClass is the famous person and you are the BugMove. When you press Follow, you do what is in the Start. From now on, any time the guy tweets, you get a feed of it.