Search Unity

Update every 2 seconds

Discussion in 'Scripting' started by davidhri, Jul 29, 2014.

  1. davidhri

    davidhri

    Joined:
    Jul 29, 2014
    Posts:
    2
    I need help

    how to make to Update every 2 seconds. (time function ?)

    Code (CSharp):
    1.  
    2. var health_exp = 13;
    3.  
    4. function OnTriggerEnter(other : Collider){
    5.         if (other.tag == "Player"){
    6.        
    7.         other.gameObject.SendMessage("ApplyHealth", health_exp, SendMessageOptions.DontRequireReceiver);
    8.         //Destroy(gameObject);
    9.     }    
    10. }
    Code (CSharp):
    1. function ApplyHealth (health_exp : float){
    2.     exp_health += health_exp;
    3.     PlayerPrefs.SetFloat("exp_health", exp_health);
    4.    // SpawnPts(health_exp,xLocationExpPlus,yLocationExpPlus);
    5. }
    thank you
     
  2. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    Coroutine or invoke
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    InvokeRepeating, rather.

    --Eric
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Depends. The player shouldn't be constantly checking his health instead when he gets hit check the state of health that triggers something. Like TakeDamage(float damageAmount) this way you don't have to worry about an update function. So after the player takes damage check is it less then or equal to zero, if so this dude is dead. You can also use this to warn the player of low health by having the HUD blink etc.
     
    Magiichan likes this.