Search Unity

InvokeRepeating Not Working

Discussion in 'Scripting' started by Blar321, Sep 3, 2015.

  1. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25
    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 :D
    Code (CSharp):
    1.     void healthRegeneration(){
    2.        
    3.         Debug.Log ("Done");
    4.         playerHealth = playerHealth + 2;
    5.     }
    6.  
    7.     void Start () {
    8.         InvokeRepeating ("healthRegeneration", 2f, 2f);
    9.         playerHealth = maxPlayerHealth;
    10.     }
    11.  
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    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?
     
  3. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25
    Code (CSharp):
    1. public class PlayerHealth : MonoBehaviour {
    2.    
    3.     public int maxPlayerHealth = 1000;
    4.     public int playerHealth = 1000;
    5.     public int lives = 3;
    6.  
    7.     public bool takingDamageSlime = false;
    8.     public bool takingDamageOgre = false;
    9.     // If the enemy is attacking  
    10.  
    11.     public void SlimeDamage(){
    12.         takingDamageSlime = true;
    13.     }
    14.     public void OgreDamage(){
    15.         takingDamageOgre = true;  
    16.     }
    17.    
    18.     void healthRegeneration(){
    19.        
    20.         Debug.Log ("Done");
    21.         playerHealth = playerHealth + 2;
    22.     }
    23.  
    24.     void Start () {
    25.         InvokeRepeating ("healthRegeneration", 2f, 2f);
    26.         playerHealth = maxPlayerHealth;
    27.     }
    28.  
    29.  
    30.     void Update () {
    31.  
    32.         int damageSlime = Random.Range(15,51);
    33.         // Amount of damage the slime causes (1.5-5%)  
    34.         if (takingDamageSlime == true) {
    35.             playerHealth = playerHealth - damageSlime;
    36.             takingDamageSlime = false;
    37.         }
    38.  
    39.         int damageOgre = Random.Range(60, 100);
    40.         // Amount of damage the slime causes (6-10%)
    41.         if (takingDamageOgre == true){
    42.             playerHealth = playerHealth - damageOgre;
    43.             takingDamageOgre = false;
    44.         }
    45.  
    46.  
    47.  
    48.         if(playerHealth <= 0){
    49.             playerHealth = 1000;
    50.             lives = lives - 1;
    51.         }
    52.  
    53.  
    54.     }
    55.  
    56. }    
    57.  
    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?
     
  4. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25