Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Invoke Repeating doesn't work

Discussion in 'Scripting' started by kqmdjc8, Jun 14, 2019.

  1. kqmdjc8

    kqmdjc8

    Joined:
    Jan 3, 2019
    Posts:
    102
    Hello, I've been using this function already and it has been working fine but this time I have no idea what am I doing wrong. Console shows debug from start method, but doesn't show anything for DisableCube method
    Code (CSharp):
    1. void Awake()
    2.     {
    3.         InvokeRepeating("DisableCube", 2.0f, 0.3f);
    4.     }
    5.  
    6.     void Start()
    7.     {
    8.         Debug.Log("I'm working! /start");
    9.     }
    10.  
    11.     IEnumerator DisableCube()
    12.     {
    13.         Debug.Log("I'm working! /method");
    14.         yield return new WaitForSeconds(1);
    15.     }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    InvokeRepeating won't run if object is not enabled.
     
  3. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    InvokeRepeating doesn't start coroutines. You would need to do something like this
    Code (csharp):
    1. void Awake()
    2. {
    3.     InvokeRepeating("DisableCube", 2.0f, 0.3f);
    4. }
    5. void DisableCube()
    6. {
    7.     StartCoroutine(DisableCubeRoutine);
    8. }
    9. IEnumerator DisableCubeRoutine()
    10. {
    11.     // Do stuff
    12.     yield return new WaitForSeconds(1.0f);
    13. }
    But if you are going to use coroutines, why not just use coroutines?
    Code (csharp):
    1. void Awake()
    2. {
    3.     StartCoroutine(DisableCube);
    4. }
    5. IEnumerator DisableCube()
    6. {
    7.     yield return new WaitForSeconds(2.0f); // Initial delay
    8.     while(true) // Or whatever condition you want
    9.     {
    10.          // Do stuff
    11.          yield return new WaitForSeconds(1.0f);
    12.     }
    13. }
     
    Last edited: Jun 14, 2019
    Ryiah and Vryken like this.
  4. kqmdjc8

    kqmdjc8

    Joined:
    Jan 3, 2019
    Posts:
    102