How to cancel an Invoke from another script,How to Cancel an invoke from another script

So basically I’m trying to cancel an InvokeRepeating call from another script using this code:

void OnTriggerExit(Collider otherCollider) {
		if (otherCollider.tag == "Player") {
			CancelInvoke ("Approached");

But the cancel Invoke doesn’t cancel it.

This is the invoke I am trying to cancel:

using UnityEngine;
using System.Collections;

public class EnemyAi : MonoBehaviour {
	
	public Transform target;
	public Transform myTransform;
	Animator anim;


	// Use this for initialization
	void Start () {

		anim = GetComponent<Animator> ();

	}
	
	// Update is called once per frame
	void Update () {


	}


	 void Approaching () {
		transform.LookAt (target);
		transform.Translate (Vector3.forward * 1 * Time.deltaTime);
		anim.SetInteger ("EnemyState", 1);
	}


		
	public void Attack () {
		anim.SetInteger ("EnemyState", 2);
		CancelInvoke ("Approached");
	}

	public void Approached () {
		InvokeRepeating ("Approaching", 0.01f ,0.01f); 
	}

,

hello, you can create a method in class EnemyAi.

 public void stopInvoke() {
     CancelInvoke (); 
 }

And invoke it from the other script:

void OnTriggerExit(Collider otherCollider) {
         if (otherCollider.tag == "Player") {
             otherCollider.SendMessage("stopInvoke");

I know this is a massive necro but this is the top Google result and there are better optimizations available. To properly cancel another MonoBehavior script’s invoke, get a reference to the script and call CancelInvoke on that.

In main script:

[SerializeField] private BombSpawnsDirector publiclyDeclaredDirector;

private void Start()
{
     bombDirector = publiclyDeclaredDirector.GetComponent<BombSpawnsDirector>();
}

private void EndRound() 
{
     bombDirector.CancelInvoke("BombSpawns");
}