• Unity
  • Services
  • Made with Unity
  • Learn
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Jun 29, 2017 at 05:29 PM 0
Share
 CancelInvoke("Approaching");

you are invoking approaching so its that one you need to cancel.

avatar image Natfran ShadyProductions · Jun 29, 2017 at 05:40 PM 0
Share

ok thank you so much

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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");

...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Solved] Is Invoke Repeating Frame Independent?? 2 Answers

InvokeRepeating() Does Not Use Repeat Rate? 1 Answer

InvokeRepeating gets cancelled on scene change? 1 Answer

Raycast ignoring InvokeRepeating intervals 0 Answers

Issue with starting/canceling invokes 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges