- Home /
Question by
Natfran · Jun 29, 2017 at 05:15 PM ·
scripting probleminvokerepeatingcancelinvoke
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);
}
,
Comment
CancelInvoke("Approaching");
you are invoking approaching so its that one you need to cancel.
Answer by gralVilla · Nov 16, 2017 at 08:07 PM
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");
...
Your answer
Follow this Question
Related Questions
InvokeRepeating gets cancelled on scene change? 1 Answer
CancelInvoke cant cancell,Updating InvokeRepeating 1 Answer
Raycast ignoring InvokeRepeating intervals 0 Answers
Issue with starting/canceling invokes 0 Answers
Need Help with changing skyboxes -1 Answers