InvokeRepeating Not Working

Hello! I am currently working on a game and I came across a problem : The InvokeRepeating function doesn’t work whatsoever. I can’t figure out whats wrong. If anyone has an idea it would be much appreciated. Thanks in advance :smile:

    void healthRegeneration(){
       
        Debug.Log ("Done");
        playerHealth = playerHealth + 2;
    }

    void Start () {
        InvokeRepeating ("healthRegeneration", 2f, 2f);
        playerHealth = maxPlayerHealth;
    }

Seems to be working fine for me, I copied your code here into a new script to test it directly.

The simplest… it’s connected to a GameObject, and is active and enabled, right?

What’s the rest of your script look like?

Can you scale the script down to just a invokerepeating call and reproduce? If it works when scaled down, at what point in trimming out unrelated code did it start working?

public class PlayerHealth : MonoBehaviour {
   
    public int maxPlayerHealth = 1000;
    public int playerHealth = 1000;
    public int lives = 3;

    public bool takingDamageSlime = false;
    public bool takingDamageOgre = false;
    // If the enemy is attacking   

    public void SlimeDamage(){
        takingDamageSlime = true;
    }
    public void OgreDamage(){
        takingDamageOgre = true;   
    }
   
    void healthRegeneration(){
       
        Debug.Log ("Done");
        playerHealth = playerHealth + 2;
    }

    void Start () {
        InvokeRepeating ("healthRegeneration", 2f, 2f);
        playerHealth = maxPlayerHealth;
    }


    void Update () {

        int damageSlime = Random.Range(15,51);
        // Amount of damage the slime causes (1.5-5%)   
        if (takingDamageSlime == true) {
            playerHealth = playerHealth - damageSlime;
            takingDamageSlime = false;
        }

        int damageOgre = Random.Range(60, 100);
        // Amount of damage the slime causes (6-10%)
        if (takingDamageOgre == true){
            playerHealth = playerHealth - damageOgre;
            takingDamageOgre = false;
        }



        if(playerHealth <= 0){
            playerHealth = 1000;
            lives = lives - 1;
        }


    }

}

Here’s the whole thing. (I know it’s bad code… I’m not finished with it). What do you think is causing it to mess up?

Bump