Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Conect11 · Sep 22, 2013 at 09:14 PM · health

Making a hunger script

Hello again,

So I'm making a hunger script that's going relatively ok except for one small hiccup. It's essentially my health script in reverse, meaning that for every step, hunger increases, with the intention that when hunger reaches a certain point, the player's health will start to decrease. So, I'm halfway there. Hunger definitely increases with each step (rate of increase and cap level of when starvation occurs is currently irrelevant to the issue, I'll adjust those later and they're intentionally set low right now to test concept) but once max hunger is reached, nothing happens to player health. (Which is actually called from the Playerhealth script, specifically Playerhealth.curHealth) Am hoping someone might be able to point me in the right direction as to how to implement this properly. Bonus points for helping me learn how to place a simple text message at different benchmarks, (hunger @ 10 = "You should get a snack" hunger at 20 "you're starving" etc) though that's not necessarily my main focus yet. Thanks, and God bless.

 static var curHunger : int = 0;
 var maxHunger : int = 35;
  
 var hungertext : GUIText;
  
  
 function Start ()
 {
     hungerRegen();
 }
  
  
 function Update ()
 {
     hungertext.text = "hunger " + curHunger + " / " + maxHunger;
 }
  
 if(curHunger == 35 )
 {
     Playerhealth.curHealth -= 10;
 }
  
 if(curHunger > maxHunger)
 {
     curHunger = maxHunger;
 }
  
 function hungerRegen ()
 {
     for(i=1;i>0;i++)
     {
         yield WaitForSeconds(10.0);
  
         if(curHunger < maxHunger)
         {
             curHunger++;
         }
     }
 }
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DannyB · Sep 22, 2013 at 09:22 PM 0
Share

Why is your if(curHunger == 35 ) outside of any function? Put this and the if statement that follows inside your hungerRegen() and it should work.

avatar image Conect11 · Sep 23, 2013 at 04:19 AM 0
Share

Danny, thanks for the advice. For some reason I didn't get email notification, that's why it took me so long to check this out. I hate to admit this, but I tried every which way to implement this, and still nothing. I completely confess that this has nothing to do with your advice, and everything to do with user error. I'm trying to learn on the fly, and sometimes things work, sometimes it takes me a bit longer to pick up concepts, for which I apologize. While I'm just starting to grasp js, a lot still (and will for a long time) elude me. I definitely see what you mean about putting the if(curhunger) statement outside of any function, but am trying to grasp which hungerRegen to put it under, the first one, or the function update? And when you say the following if statement, I am wondering if you mean just the entirety of the if curHunger == 35 along with "PlayerHealth.curHealth," or if you're also meaning the curHunger > $$anonymous$$axHunger below it. I am so sorry, I know these are noob questions, and I am really trying to learn and not be a knowledge mooch here.

avatar image OperationDogBird · Sep 23, 2013 at 05:30 AM 1
Share

First thing you should learn when coding in SCOP$$anonymous$$ ex.

 if(myBool) DoSomething();
 DoAnotherThing();

The method call 'DoAnotherThing' is not in the same scope as 'DoSomething' in relation to the condition 'if myBool is true'.

This is what @DannyB is referring to, except in the context of a method/function.

 function Update() 
 { 
     DoSomething(); 
     //Code cannot be outside of function brackets
 }
 if(someCondition) // this will cause a compiler error

There are some exceptions to that rule, all of which should be at the top of your script. For example: Global Variables, statements such as #pragma, and using/import statements ( i may be forgetting some ).

I suggest learning c# as it is much more strict (much more difficult to learn at first), thus you will learn the proper way to code from the start, vs. trying to unlearn nasty habits :) But there is nothing wrong with javascript, there is just more support for c# across the board and encourages better coding.

avatar image syclamoth · Sep 23, 2013 at 05:36 AM 1
Share
  • about using C#- it is in general less confusing than UnityScript, not to mention generally better supported. In UnityScript, any code that is written outside of any functions is considered to be inside what would be in C# the 'Awake' function- that is to say, it will execute once, when the object is loaded, and then never again. This means that if you are checking the player's current hunger in 'Awake' it will never be above zero!

EDIT I just cleaned up the code in the original question, and the problem is suddenly much more obvious. $$anonymous$$ake sure you put the conditionals that check current hunger level inside of Update- otherwise they won't get checked every frame!

avatar image OperationDogBird · Sep 23, 2013 at 05:46 AM 0
Share
  • for the additional line "any code that is written outside of any functions is considered to be inside what would be in C# the 'Awake'"

1 more note before i leave you to your tasks.

C# and javascript use such a similar syntax that it is better to learn c# now, then migrate to js later rather than vice versa, just because you wont be tempted to develop bad habits. This is the most common 'nasty habit' i experience when converting other peoples javascript/unityscript

  function Update()
  {
      var i;
      var u;
      // hundreds of lines of code!!
  }

Looking at this code, it is impossible to deter$$anonymous$$e what the type of i & u are without digging through the additional hundreds of lines of code. Even then, you need very good intellisense or tracking skills to figure the proper types of those variables.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Conect11 · Sep 23, 2013 at 04:14 PM

Answered through pieces together lessons learned in comments. Here's what the script looks like:

 static var curHunger : int = 0;
 var maxHunger : int = 35;
  
 var hungertext : GUIText;
  
  
 function Start ()
 {
 hungerRegen();
 }
  
  
 function Update ()
 {
 hungertext.text = "hunger " + curHunger + " / " + maxHunger;
 
  if(curHunger == 35 )
 Playerhealth.curHealth -= 1;
 
 }
  
 
  
 if(curHunger > maxHunger)
 {
 curHunger = maxHunger;
 }
  
 
  
 function hungerRegen ()
 {
 for(i=1;i>0;i++)
 {
 yield WaitForSeconds(10.0);
  
 if(curHunger < maxHunger)
 {
 curHunger++;
 }
 }
 }

Note: speed of health loss has not been adjusted yet.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

this is part of my health script it gives me a error unknown identifier GameScore pls help thxxx 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Problem with Pause and Pause Menu Script 1 Answer

update highscore if current is higher than previous score 0 Answers

Heath system need help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges