Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Making Hunger Script & Health Script Work Together ??

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

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    So what i want to happen is the following:
    : When the hunger bar countdown from 100 reaches 0, it should tell the health script to activate "NoHunger();", which sets "PlayerIsHungry to True" And then the health should drop by the amount listed at the Var "HungerDamage" each second until its down at 0 and then the player should die.

    But when the hunger bar reaches 0, nothing happens...
    Please help me, code listed below!

    Hunger Script

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var MaxHunger = 100;
    4. var Hunger : float;
    5.  
    6. function Start ()
    7. {
    8.     Hunger = MaxHunger;
    9.     InvokeRepeating("GettingHungry", 0, 5);
    10. }
    11.  
    12. function GettingHungry ()
    13. {
    14.     Hunger --;
    15.    
    16.     if(Hunger<=0)
    17.     {
    18.         CancelInvoke("GettingHungry");
    19.         {
    20.             NoHunger();
    21.         }
    22.     }
    23. }
    24.  
    25. function RespawnStats ()
    26. {
    27.     Hunger = MaxHunger;
    28. }
    29.  
    30. function OnGUI()
    31. {
    32.     GUI.Box(new Rect(10, 10, 50, 20), "" + Hunger.ToString("0"));
    33. }
    34.  
    35. function Dead()
    36. {
    37.     RespawnMenuV2.playerIsDead = true;
    38.     Debug.Log("Player Died");
    39. }
    40.  
    41. function NoHunger()
    42. {
    43.     PlayerHealth.PlayerIsHungry = true;
    44. }
    Health Script

    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4. public var MaxHealth = 100;
    5. public var Health : float;
    6. static var PlayerIsHungry = false;
    7.  
    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. function LoosingHealth()
    43. {
    44.     if (PlayerIsHungry==true)
    45.     {
    46.         Health -= HungerDamage;
    47.         InvokeRepeating("LoosingHealth", 3, 3);
    48.     }
    49.     if (Health<=0)
    50.     {
    51.         CancelInvoke("LoosingHealth");
    52.         Dead();
    53.     }
    54. }
    Dont worry about the Dead(); function, it´s in another script and that works, its the health dropping that doesnt like me :b
     
  2. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    This doesn't look right...

    Code (JavaScript):
    1. if(Hunger<=0)
    2.     {
    3.         CancelInvoke("GettingHungry");
    4.         {
    5.             NoHunger();
    6.         }
    7.     }
    Shouldn't it be

    Code (JavaScript):
    1. if(Hunger<=0)
    2.     {
    3.         CancelInvoke("GettingHungry");
    4.         NoHunger();        
    5.     }
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
  5. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    i just changed it as you said and it didn´t make any difference
     
  6. Hard target

    Hard target

    Joined:
    Oct 31, 2013
    Posts:
    132
    I was think about a videogame using the same thing.But with driveable vehicles.What type of game are you making?
     
  7. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Survival PvP :)

    And i´m trying to make a hunger system that affects the player health once it hits 0, like in minecraft etc.
     
  8. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Move line 47 in Health to 13.
     
  9. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    I wouldn't recommend using static. You should use Unity as it was intended with GetComponent. Making all of those properties non-static and retrieved through GetComponent will allow you to use the exact scripts on other entities -> Enemies if they have health or hunger issues as well. Also if you plan to attempt multiplayer I'm not sure how static vars would handle.

    I can see a problem as you stated. Your health isn't going down when you are hungry.. and currently you can't even get hungry again after you do eat food.


    I don't use unityScript and I can't test this since I'm at work. But you can do something like this:

    Code (CSharp):
    1. #pragma strict
    2. var MaxHunger = 100;
    3. var Hunger : float;
    4. // This is an Option to do besides Invoke repeating. Simple timer.
    5. var _hungerDelay : float = 3;
    6. var _lastHungerApplied : float = 0;
    7. var _EntityHealth :
    8.  
    9. function Start ()
    10. {
    11.     Hunger = MaxHunger;
    12.     //Make sure the script is applied to the same gameObject.
    13.     _EntityHealth = GetComponent("Health");
    14.     if(_EntityHealth == null)
    15.     {
    16.         Debug.Log("Attach Health to this GameObject: " + gameObject.name)
    17.     }
    18. }
    19. function Update()
    20. {
    21.     ConsumeNurishmentCheck();
    22. }
    23.  
    24. function ConsumeNurishmentCheck ()
    25. {
    26.     // No need to check unless we have nurishment(hunger) remaining.
    27.     if(Time.time > _lastHungerApplied && Hunger > 0)
    28.     {
    29.         Hunger --;
    30.         if(Hunger <= 0)
    31.         {
    32.             _EntityHealth.IsHungery(true);
    33.         }
    34.         _lastHungerApplied = Time.Time + _hungerDelay;
    35.     }
    36. }
    37. function RespawnStats ()
    38. {
    39.     Hunger = MaxHunger;
    40. }
    41. function OnGUI()
    42. {
    43.     GUI.Box(new Rect(10, 10, 50, 20), "" + Hunger.ToString("0"));
    44. }
    45. function Dead()
    46. {
    47.     RespawnMenuV2.playerIsDead = true;
    48.     Debug.Log("Player Died");
    49. }
    50.  
    Code (CSharp):
    1. #pragma strict
    2. public var MaxHealth : float = 100;
    3. public var Health : float;
    4.  
    5. // This is an Option to do besides Invoke repeating. Simple timer.
    6. var _hungerDamageDelay : float = 3;
    7. var _lastHungerDamageApplied : float = 0;
    8.  
    9. private var _IsHungry = false;
    10. function IsHungery(isHungry : boolean)
    11. {
    12.     //If the player wasn't hungry but now is. We should reset the timer so it doesn't immediately hurt.
    13.     if(!_IsHungry && isHungry)
    14.     {
    15.         //Resetting the timer to wait 3 seconds before applying.
    16.         _lastHungerDamageApplied = Time.Time + _hungerDamageDelay;
    17.     }
    18.     _IsHungry = isHungry;
    19. }
    20. var HungerDamage = 10;
    21. function Start ()
    22. {
    23.     Health = MaxHealth;
    24. }
    25. function ApplyDamage (TheDamage : int)
    26. {
    27.     Health -= TheDamage;
    28.  
    29.     if(Health <= 0)
    30.     {
    31.         Dead();
    32.     }
    33. }
    34.  
    35.  
    36. function Update()
    37. {
    38.     //Check if we should lose health. This can happen each frame.
    39.     CheckForHungerHealthLoss();
    40. }
    41.  
    42. function OnGUI()
    43. {
    44.     GUI.Box(new Rect(65, 10, 50, 20), "" + Health.ToString("0"));
    45. }
    46. function Dead()
    47. {
    48.     RespawnMenuV2.playerIsDead = true;
    49.     Debug.Log("Player Died");
    50. }
    51. //Respawn Full Health
    52. function RespawnStats ()
    53. {
    54.     Health = MaxHealth;
    55. }
    56. function CheckForHungerHealthLoss()
    57. {
    58.     if (_IsHungry)
    59.     {
    60.         if(Time.time > _lastHungerDamageApplied)
    61.         {
    62.             //Make use of your built in methods.. no need to redo code.
    63.             ApplyDamage(HungerDamage);
    64.             _lastHungerDamageApplied = Time.time + _hungerDamageDelay;
    65.         }
    66.     }
    67. }
     
  10. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    That will make it start reducing the players health from the beginning :/
     
  11. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Thanks for the fine scripting, but i must admit that i cant fully understand it all sadly :i
    And i´m trying to make a game out of my own scripts, and its hard for me to edit that later on when i dont even know what it does.
    Why do you use Csharp, is it better than JS?
     
  12. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Why not just try to use it and learn? It's actually very basic concepts nothing crazy yet. I just prefer c#.

    If you don't understand how to use Update you should at least learn what it does which is basically and endless loop one of the three built in loops(Update, FixedUpdate, LateUpdate) each has a purpose. Such as Update is good for Input commands -> if(Input.GetKeyDown(KeyCode.P)) {//Do stuff} . FixedUpdate is good to run your physics in. Late is good for Animations, I think.

    Time.time is nothing more then a counter of time since the game has started. So if you did delayTime = Time.time + 3; that would be 3 seconds from the current game time. Time.time > delayTime is just a check to see when your time delay has been broken or not.
     
  13. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    ahhh thanks, but there is a bunch of errors i get when putting your version in but i have corrected and fixed them all except 1 tricky guy, the following:
    var EntityHealth :
    &
    EntityHealth.IsHungry(true);

    Does Together give me the following errors:

    9,10): BCE0044: expecting (, found 'Start'.

    (9,15): UCE0001: ';' expected. Insert a semicolon at the end.

    (9,16): BCE0044: expecting EOF, found '('.

    please help :)
     
  14. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Whoops not sure how I missed typing the Health area. I think I went to look at your script name but forgot to add it in after.

    Code (CSharp):
    1. #pragma strict
    2. var MaxHunger = 100;
    3. var Hunger : float;
    4. // This is an Option to do besides Invoke repeating. Simple timer.
    5. var _hungerDelay : float = 3;
    6. var _lastHungerApplied : float = 0;
    7. var _EntityHealth : Health; // This needs to be your script name for the Health.
    8. function Start ()
    9. {
    10.     Hunger = MaxHunger;
    11.     //Make sure the script is applied to the same gameObject.
    12.     _EntityHealth = GetComponent("Health");
    13.     if(_EntityHealth == null)
    14.     {
    15.         Debug.Log("Attach Health to this GameObject: " + gameObject.name)
    16.     }
    17. }
    18. function Update()
    19. {
    20.     ConsumeNurishmentCheck();
    21. }
    22. function ConsumeNurishmentCheck ()
    23. {
    24.     // No need to check unless we have nurishment(hunger) remaining.
    25.     if(Time.time > _lastHungerApplied && Hunger > 0)
    26.     {
    27.         Hunger --;
    28.         if(Hunger <= 0)
    29.         {
    30.             //This is your reference to the Health Script. We set the boolean here.
    31.             //I typoed the name btw so change it in both places.
    32.             _EntityHealth.IsHungry(true);
    33.         }
    34.         _lastHungerApplied = Time.Time + _hungerDelay;
    35.     }
    36. }
    37. function RespawnStats ()
    38. {
    39.     Hunger = MaxHunger;
    40. }
    41. function OnGUI()
    42. {
    43.     GUI.Box(new Rect(10, 10, 50, 20), "" + Hunger.ToString("0"));
    44. }
    45. function Dead()
    46. {
    47.     RespawnMenuV2.playerIsDead = true;
    48.     Debug.Log("Player Died");
    49. }
     
  15. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    I have changed all the codes with an "_"before them like
    before: _lastHungerApplied
    Now: lastHungerApplied
    Does that make any bugs or is it okay?

    And after i changed the "var EntityHealth : Health;"
    i now get the error: "(7,20): BCE0018: The name 'Health' does not denote a valid type ('not found'). "
     
  16. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Just read my comment. That is the health script. That is a reason I'm not fond of JS.. class names are even present. Whatever your class name is which I have no idea since you created it. Change "Health" to whatever that is "PlayerHealth" or whatever it is.

    The _ is a preference of private variables. Do whatever you feel comfortable with.
     
    Last edited: Jul 25, 2014
    Quist likes this.
  17. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    What Entity am i supposed to drag to the "Entity Health"?
    My best guess would be my player, but when i do that it says
    :
    NullReferenceException: Object reference not set to an instance of an object
    LOL.ConsumeNurishmentCheck ()
     
  18. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Code (CSharp):
    1. #pragma strict
    2. public var MaxHealth : float = 100;
    3. public var Health : float;
    4. // This is an Option to do besides Invoke repeating. Simple timer.
    5. var _hungerDamageDelay : float = 3;
    6. var _lastHungerDamageApplied : float = 0;
    7. private var _IsHungry = false;
    8. function IsHungery(isHungry : boolean)
    9. {
    10.     //If the player wasn't hungry but now is. We should reset the timer so it doesn't immediately hurt.
    11.     if(!_IsHungry && isHungry)
    12.     {
    13.         //Resetting the timer to wait 3 seconds before applying.
    14.         _lastHungerDamageApplied = Time.Time + _hungerDamageDelay;
    15.     }
    16.     _IsHungry = isHungry;
    17. }
    18. var HungerDamage = 10;
    19. function Start ()
    20. {
    21.     Health = MaxHealth;
    22. }
    23. function ApplyDamage (TheDamage : int)
    24. {
    25.     Health -= TheDamage;
    26.     if(Health <= 0)
    27.     {
    28.         Dead();
    29.     }
    30. }
    31. function Update()
    32. {
    33.     //Check if we should lose health. This can happen each frame.
    34.     CheckForHungerHealthLoss();
    35. }
    36. function OnGUI()
    37. {
    38.     GUI.Box(new Rect(65, 10, 50, 20), "" + Health.ToString("0"));
    39. }
    40. function Dead()
    41. {
    42.     RespawnMenuV2.playerIsDead = true;
    43.     Debug.Log("Player Died");
    44. }
    45. //Respawn Full Health
    46. function RespawnStats ()
    47. {
    48.     Health = MaxHealth;
    49. }
    50. function CheckForHungerHealthLoss()
    51. {
    52.     if (_IsHungry)
    53.     {
    54.         if(Time.time > _lastHungerDamageApplied)
    55.         {
    56.             //Make use of your built in methods.. no need to redo code.
    57.             ApplyDamage(HungerDamage);
    58.             _lastHungerDamageApplied = Time.time + _hungerDamageDelay;
    59.         }
    60.     }
    61. }

    Whatever this script's name is. You need to change that variable type to that. Maybe you should look at more examples of how to use components in Unity3d? http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
     
  19. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    mate... I have changed it to my health scripts name "PlayerHealth", but the same errors pops up :) ?
     
  20. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Post a screen shot of the gameObject(Inspector view)
     
  21. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
     
  22. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
  23. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Odd names but whatever:
    Use these scripts. Fix any typo's, remove underscores if you want. But you need the LOLHEALTH just as I have it.. and the same goes for the _EntityHealth.IsHungry(true) call. (You can change _EntityHealth to entityHealth if you wish but update it everywhere)

    Code (CSharp):
    1. #pragma strict
    2. var MaxHunger = 100;
    3. var Hunger : float;
    4. // This is an Option to do besides Invoke repeating. Simple timer.
    5. var _hungerDelay : float = 3;
    6. var _lastHungerApplied : float = 0;
    7. var _EntityHealth : LOLHEALTH; // This needs to be your script name for the Health.
    8. function Start ()
    9. {
    10.     Hunger = MaxHunger;
    11.     //Make sure the script is applied to the same gameObject.
    12.     _EntityHealth = GetComponent("LOLHEALTH");
    13.     if(_EntityHealth == null)
    14.     {
    15.         Debug.Log("Attach Health to this GameObject: " + gameObject.name)
    16.     }
    17. }
    18. function Update()
    19. {
    20.     ConsumeNurishmentCheck();
    21. }
    22. function ConsumeNurishmentCheck ()
    23. {
    24.     // No need to check unless we have nurishment(hunger) remaining.
    25.     if(Time.time > _lastHungerApplied && Hunger > 0)
    26.     {
    27.         Hunger --;
    28.         if(Hunger <= 0)
    29.         {
    30.             //This is your reference to the Health Script. We set the boolean here.
    31.             //I typoed the name btw so change it in both places.
    32.             _EntityHealth.IsHungry(true);
    33.         }
    34.         _lastHungerApplied = Time.Time + _hungerDelay;
    35.     }
    36. }
    37. function RespawnStats ()
    38. {
    39.     Hunger = MaxHunger;
    40. }
    41. function OnGUI()
    42. {
    43.     GUI.Box(new Rect(10, 10, 50, 20), "" + Hunger.ToString("0"));
    44. }
    45. function Dead()
    46. {
    47.     RespawnMenuV2.playerIsDead = true;
    48.     Debug.Log("Player Died");
    49. }
    Code (CSharp):
    1. #pragma strict
    2. public var MaxHealth : float = 100;
    3. public var Health : float;
    4. // This is an Option to do besides Invoke repeating. Simple timer.
    5. var _hungerDamageDelay : float = 3;
    6. var _lastHungerDamageApplied : float = 0;
    7. private var _IsHungry = false;
    8. function IsHungry(isHungry : boolean)
    9. {
    10.     //If the player wasn't hungry but now is. We should reset the timer so it doesn't immediately hurt.
    11.     if(!_IsHungry && isHungry)
    12.     {
    13.         //Resetting the timer to wait 3 seconds before applying.
    14.         _lastHungerDamageApplied = Time.Time + _hungerDamageDelay;
    15.     }
    16.     _IsHungry = isHungry;
    17. }
    18. var HungerDamage = 10;
    19. function Start ()
    20. {
    21.     Health = MaxHealth;
    22. }
    23. function ApplyDamage (TheDamage : int)
    24. {
    25.     Health -= TheDamage;
    26.     if(Health <= 0)
    27.     {
    28.         Dead();
    29.     }
    30. }
    31. function Update()
    32. {
    33.     //Check if we should lose health. This can happen each frame.
    34.     CheckForHungerHealthLoss();
    35. }
    36. function OnGUI()
    37. {
    38.     GUI.Box(new Rect(65, 10, 50, 20), "" + Health.ToString("0"));
    39. }
    40. function Dead()
    41. {
    42.     RespawnMenuV2.playerIsDead = true;
    43.     Debug.Log("Player Died");
    44. }
    45. //Respawn Full Health
    46. function RespawnStats ()
    47. {
    48.     Health = MaxHealth;
    49. }
    50. function CheckForHungerHealthLoss()
    51. {
    52.     if (_IsHungry)
    53.     {
    54.         if(Time.time > _lastHungerDamageApplied)
    55.         {
    56.             //Make use of your built in methods.. no need to redo code.
    57.             ApplyDamage(HungerDamage);
    58.             _lastHungerDamageApplied = Time.time + _hungerDamageDelay;
    59.         }
    60.     }
    61. }
     
    Quist likes this.
  24. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    I have fixed the odd names now, sorry, i had to go back then and just had to do a fast name and check if it worked.

    It F***ing works now dude!!!!

    Thanks a lot for taking the time to help a noob like me :)
    Btw, how did you get so good at it ? I´m currently 15 and i wish that i someday can live by developing games, do you have any suggestions or something ? :)
     
  25. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    I've been using Unity for 4 maybe 5 years now. It just takes practice. Try to participate in Unity's Scripting forums. Read them and the solutions people provide and when you feel you know the answer to a question try to answer it.

    Your starting early though so you have quite an advantage on someone like me who started Unity around the age of 24. If you can stick with it of course. Even I don't do this for a living. I'm a Software developer so I can't tell you how to get that goal. I know Hippocoder has posted some stuff about how to make a decent income with making games on the forums. From what I remember is that you just have to keep pumping out games quickly.. never get stuck on these slow games. I think he does mostly mobile as well.
     
  26. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    LOL No it works I checked it, but good to see you've got it sorted.
     
  27. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    I just found out there is a minor problem i didn´t know before now that would come up..
    When the Hunger hits 0 and it starts reducing the players health, it continues to go down to minus, and even when i respawn the player and he got 100 health it keeps making him loose -10 health each second, any suggestions?
    My best looked like the following but it didnt work:
    Code (JavaScript):
    1. function CheckForHungerHealthLoss()
    2. {
    3.     if (_IsHungry)
    4.     {
    5.         if(Time.time > _lastHungerDamageApplied)
    6.         {
    7.             ApplyDamage(HungerDamage);
    8.             _lastHungerDamageApplied = Time.time + _hungerDamageDelay;
    9.         }
    10.         if(Health<=0)
    11.         {
    12.             ApplyDamage(HungerDamage) = false;
    13.         }
    14.        
    15.     }
    16. }
     
  28. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    It's not the health that is the issue its the Hunger that needs to be reset and the boolean _isHungry needs to be reset to false.

    You shouldn't need that additional check -> if(Health<=0) because your ApplyDamage handles that by itself.
     
  29. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    As always, i did what you just said and it worked, perfect and informative mate ! :)

    I have understood it all nearly except the following stuff:
    If you could explain what they do to me it would help a lot! :)

    1) _EntityHealth = GetComponent("PlayerHealthV2");
    : I understand that you get a component from the other script, but does "_EntityHealth" mean that you get the PlayerHealthV2 scripts Health var?

    2) I noticed all the underscores before some stuff, why have them why not have them?

    3) if(_EntityHealth == null)
    :
    again, what is the "EntityHealth", and what does it mean when its equal to null?

    4) What is it in the hunger script that makes it start precisely at 100 to countdown from?
     
  30. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    1) _EntityHealth = GetComponent("PlayerHealthV2");
    _EntityHealth is just a variable that holds a reference of that script. So you can access that specific scripts public Methods and variables. Example: _EntityHealth.ApplyDamage(5); // This is a valid call using that reference.

    Remember this is specific to that gameObject only. So an entity(player/enemy) hunger is their own. If it was static it would not be.

    2) I noticed all the underscores before some stuff, why have them why not have them?
    I actually answered this up above: The _ is a preference of private variables. Do whatever you feel comfortable with.

    3) if(_EntityHealth == null)
    Since _EntityHealth is just a variable and we assign it at start (Instead of in the inspector which is an option). We need to be sure that its not null. Nulls will just throw null references exceptions constantly while trying to access the variable since it was never properly set. Its good practice to do null checks before using such variables.

    4) What is it in the hunger script that makes it start precisely at 100 to countdown from?
    Code (CSharp):
    1.  
    2. var MaxHunger = 100;
    3. var Hunger : float;
    4. function Start ()
    5. {
    6.     Hunger = MaxHunger; // Your setting the Hunger to 100 here.
    7. }
    8.  
    9. function Update()
    10. {
    11.     //This checks each frame to see if we should decrease our nourishment (add hunger)
    12.     ConsumeNurishmentCheck();
    13. }
    Start using Debug.Log() often to see what values are returning. **Caution** clean them up after you see whats happening. They can cause huge lag if left in an update. Example of Debug.Log:

    Code (CSharp):
    1. function ConsumeNurishmentCheck ()
    2. {
    3.     // No need to check unless we have nurishment(hunger) remaining.
    4.     if(Time.time > _lastHungerApplied && Hunger > 0)
    5.     {
    6.         Hunger --;
    7.  
    8.         if(Hunger <= 0)
    9.         {
    10.             //This is your reference to the Health Script. We set the boolean here.
    11.             //I typoed the name btw so change it in both places.
    12.             _EntityHealth.IsHungry(true);
    13.         }
    14.         _lastHungerApplied = Time.Time + _hungerDelay;
    15.         Debug.Log("Decreasing hunger: " + Hunger + "  |  Next Hunger Decrease: " + _lastHungerApplied);
    16.     }
    17. }
     
  31. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    If you want to be able to reset the hunger you can do something like this:
    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.     // Null check practice:
    5.     // No code will be ran if _EntityHealth is not set correctly.
    6.     if(_EntityHealth == null)
    7.         return;
    8.  
    9.     //If we press button H. Set Hunger to max.
    10.     if(Input.GetKeyDown(KeyCode.H))
    11.     {
    12.         _EntityHealth.IsHungry(false);
    13.         Hunger = MaxHunger;
    14.     }
    15.  
    16.     ConsumeNurishmentCheck();
    17. }
    18.  
     
  32. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    i actually tried and fixed the restart hunger problem yesterday doing the following:

    Code (JavaScript):
    1. function CheckForHungerHealthLoss()
    2. {
    3.     if (_IsHungry)
    4.     {
    5.         if(Time.time > _lastHungerDamageApplied)
    6.         {
    7.             ApplyDamage(HungerDamage);
    8.             _lastHungerDamageApplied = Time.time + _hungerDamageDelay;
    9.         }
    10.         if(Health<=0)
    11.         {
    12.             RestartHunger();
    13.         }
    14.     }
    15. }
    16.  
    17. function RestartHunger ()
    18. {
    19.     if(Health<=0)
    20.     {
    21.         _IsHungry = false;
    22.     }
    23. }
    It actually works and im so happy!! xD

    But how would you rate that coding solution from 1 - 10? Like in how good it is :3