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 hdy · Jan 14, 2012 at 09:42 PM · timecomparedecreasetimespan

TimeSpan why are you being difficult?

Question updated, 1/17/12 11:30 am EST.

What i am trying to do is to make the pet's hunger(or food) decrease every 12 hours

so the user/player can feed it every now and then.

any help would be much appreciated.

  var monsterCurrentHunger: int = 100;
  var monsterMaxHunger: int = 100;
  var hungerTimerStarted: boolean = false;
  var monsterAlive: boolean = true;
  var saveTime: System.DateTime;
  var passedTime: System.TimeSpan;
  var hungryTime: System.TimeSpan; // need to make this into a constant of 12 hours
  function Update () {
      // recording the timer or start point
      if (hungerTimerStarted == false){
      saveTime = System.DateTime.Now;
      hungerTimerStarted = true;
      }
      // once timer started 
      if (hungerTimerStarted == true){
      passedTime = System.DateTime.Now - saveTime;  

          // every 12 hours hunger lose 10 point.
     /*error: This code here is what i want to do and don't know how to do it.

1, how to create a constant timespan of 12 hours? (the hungryTime var on top)

2, how to do the 3 line below:

  • //error msg end

         if (passedTime => hungryTime) {
    
           monsterCurrentHunger -= (10 * (passedTime/hungryTime));
    
               hungerTimerStarted = false;
    
           }
    
          }
    
      }
    
Comment
Add comment · Show 5
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 DaveA · Jan 17, 2012 at 02:52 AM 0
Share

Please edit your code, select it, hit the 101010 button to format it please.

avatar image flamy · Jan 17, 2012 at 06:19 AM 0
Share

can you please say what the errors are?? it is clueless for us... btw is is compiler error or logical error ?!?!

avatar image hdy · Jan 17, 2012 at 06:47 AM 0
Share

edited it hope it is easier to read now, and added the compiler error

avatar image flamy · Jan 17, 2012 at 09:55 AM 0
Share

passed time is of type System.DateTime you have to change it to timespan because supraction of 2 times returns a time span not another time!! and also the other error is because u used - ins$$anonymous$$d of -=

avatar image hdy · Jan 17, 2012 at 04:34 PM 0
Share

Thanks for the help, i really appreciated it.

when i changed the passedTime to TimeSpan, it took care of the error, but now i am getting more road block towards what i want to do. the Question has been edited.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Default117 · Jan 17, 2012 at 06:54 AM

I answered a very similar question a while ago regarding real time integration with games and affecting "hunger" =P http://answers.unity3d.com/questions/171302/save-current-time-on-quit-then-on-relaunch-compare.html

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
avatar image
1

Answer by BiG · Jan 17, 2012 at 07:04 AM

Dewald117's solution seems a great one. This would solve the first error. About the second error of yours,

 monsterCurrentHunger - (passedTime).Day * 20;

doesn't seem a legal statement (that's not an assignment). Would you say

 monsterCurrentHunger -= (passedTime).Day * 20;

, maybe?

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

8 People are following this question.

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

Related Questions

slowly decrease value when in trigger 1 Answer

See if a time span is bigger than X 4 Answers

Increase/Decrease Over Time, Not Frames 1 Answer

Accumulating deltaTime oddity 1 Answer

Unity Simple Clock Tutorial with custom time? 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