Search Unity

InvokeRepeating Not Funtioning

Discussion in 'Scripting' started by Blar321, Aug 31, 2015.

  1. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25
    Hello everybody. I wrote this script which is supposed to make healthScript.SlimeDamage(); run every so often but it only executes once . What did I do wrong?

    Code (CSharp):
    1.     void attackTimer(){
    2.         slimeBasicAttack = true;  
    3.     }
    4.  
    5.     void Update () {
    6.         InvokeRepeating ("attackTimer", 100, 1000f);
    7.  
    8.         GameObject health = GameObject.Find ("Hero");
    9.         //"Hero" can be replaced by name of GameObject that the the Health script is on
    10.         Health healthScript = (Health)health.GetComponent (typeof(Health));
    11.        
    12.        
    13.         if (slimeBasicAttack == true && dodge == false) {
    14.             healthScript.SlimeDamage ();
    15.             slimeBasicAttack = false;
    16.         }
    17.     }
    18. }
    19.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're calling InvokeRepeating every frame in Update. It should be called only once.

    --Eric
     
  3. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25
    It still doesnt work :/
     
  4. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Did you try putting it in the "Start" function Blar321?
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,794
    You are calling it after 100seconds and then you are asking it to repeat after a thousand seconds. Are you sure you are waiting long enough to see if it's repeating?
     
  6. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25
    +//
    Yes.. Still nothing :/
     
  7. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25
    Yeah.. I changed it to 10 and 100 and still nothing
     
  8. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    I`m guessing either you still never waited 100 seconds to see the repeat, or the code broke in healthScript.SlimeDamage () method. Its bad code though really. You should cache the health and healthscript in the start function. You could even move the healthScript.SlimeDamage () call directly into the attackTimer method.
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    1. change it to 5 and 5 just to make sure its running.
    2. put a Debug.Log into attackTimer