Search Unity

Connection between scripts problem

Discussion in 'Scripting' started by Quist, Jul 24, 2014.

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Last edited: Jul 25, 2014
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
  3. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    ya if you want every second you would need to multiple by Time.deltaTime
     
  4. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    The reason your Dead function isn't triggering is because your ApplyDamage function is never called, instead you're just deducting the health directly in your Update function.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    yup this...

    but I think there is also a problem with the design here. It would make more sense for the hunger script to call "ApplyDamage(hungerDamage)" and have the damage rate from being hungry kept in the hunger script.

    That way the health script doesn't need to know anything about "hungry", "ishungry" etc. (wtf is food?? I'm a healthscript!!).

    Try to keep the scripts focused on what they are doing. It'll keep things much cleaner and you wont run into so many issues later on when tweaking or extending the functionality.
     
    NomadKing likes this.
  6. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    I have changed the script completely so it uses invokeRepeating now... :)

    But now i have a problem with the functions.

    it looks like this:

    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4. public var MaxHealth = 100;
    5. public var Health : float;
    6.  
    7. static var NoHunger = false;
    8. var HungerDamage = 10;
    9.  
    10. function Start ()
    11. {
    12.     Health = MaxHealth;
    13. }
    14.  
    15. function ApplyDamage (TheDamage : int)
    16. {
    17.     Health -= TheDamage;
    18.    
    19.     if(Health <= 0)
    20.     {
    21.         Dead();
    22.     }
    23. }
    24.  
    25. function OnGUI()
    26. {
    27.     GUI.Box(new Rect(65, 10, 50, 20), "" + Health.ToString("0"));
    28. }
    29.  
    30. function Dead()
    31. {
    32.     RespawnMenuV2.playerIsDead = true;
    33.     Debug.Log("Player Died");
    34. }
    35.  
    36. //Respawn Full Health
    37. function RespawnStats ()
    38. {
    39.     Health = MaxHealth;
    40. }
    41.  
    42. //Hungry And Taking Damage
    43. function LoosingHealth ()
    44. {
    45.     Health -= HungerDamage;
    46.    
    47.     if(Health<0)
    48.     {
    49.         CancelInvoke("LoosingHealth");
    50.         {
    51.             Dead();
    52.         }
    53.     }
    54. }
    55.  
    The problem is that if i put the follow into an "Function Update ()", it goes completly crazy and my players health goes down as soon as the game starts and not when the NoHunger is equal to true...
    What kind of Function should i put the invokerepeating in?
    Code (JavaScript):
    1. function Update()
    2. {
    3.     if(NoHunger == true)
    4.     InvokeRepeating("LoosingHealth", 1, 3);
    5. }
    6.  
     
  7. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    bump
     
  8. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    so what would i have to type instead
     
  9. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Put the InvokeRepeating("LoosingHealth",1,3); line in the start function.
     
    NomadKing likes this.