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 grimreaperpt · Mar 14, 2014 at 08:41 PM · variabletime

Problem with time system and variables

I'm currently programming a small game in Unity where the actions function in buttons. The player can pass the time using actions such as sleep, and there are variables that should change with time, such as hunger that decreases with time. The problem here is, I want the hunger to decrease by 1 point every 2 hours, and the sleep button advances the game by 8 hours. In my code the hunger decreases only if the time variable is exactly that specified number, and doesn't add up, so even if the time passes by 12 hours it only counts as 1 point of hunger. Any help?

 var hunger : int = 100;
 var getHungryOn : int = 2;
 var timeToEatCheck : boolean;
 var getTimeScript : time;
 
 function Update () {
 
 if (getTimeScript.currentHour%getHungryOn == 0 && timeToEatCheck == false)
     {
     timeToEatCheck = true;
     print ("food");
     }
     
 if (getTimeScript.currentHour%getHungryOn != 0)
     {
     timeToEatCheck = false;
     print ("no food");
     }
 
 }
Comment
Add comment · Show 2
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 superluigi · Mar 14, 2014 at 09:24 PM 0
Share

Your question kinda confuses me. Like the code up top has no sleep button. I had a problem myself with an if statement that read if(variable == Time.time). If you display time you will see like 5 or 6 numbers for the milliseconds. I think Unity just jumps over certain "times" in between frames and causes it to miss some if statements.

avatar image grimreaperpt · Mar 14, 2014 at 11:00 PM 0
Share

Well, the code I posted is just some part of the script. The other scripts I thought they wouldn't be necessary for this question. The thing is, the game isn't real time so time isn't actually running. The game is event-based, so time only passes when certain buttons are pressed.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by superluigi · Mar 14, 2014 at 09:47 PM

Edited to better fit OP's needs.

 var hours : int = 1;
 var fullStomachTime : int = 0;
 var hunger : int = 100;
 
 function Update()
 {
    if(Input.GetButton("Attack"))
    {
       hour += 1;
    }
    if(Input.GetButton("Sleep"))
    {
       hour += 8;
    }
    if(Input.GetButton("Eat"))
    {
       hunger = 100;
       fullStomachTime = hour;
    }
    if(hour == fullStomachTime + 2)
    {
       hunger -= 10;
    }
    if(hour == fullStomachTime + 4)
    {
       hunger -= 20;
    }
    if(hunger <= 0)
    {
       Destroy(gameObject);
    }
 }

This is just to give you a general idea. I believe this would work. For neatness you would want to create a Hunger function to place all of the if(hour == fullStomachTime + _) inside of it. You could put that function at the bottom of your page and just place the function in your Update function. If you need any help understanding any of this let me know, I'll be more than happy to help

Comment
Add comment · Show 3 · 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 grimreaperpt · Mar 14, 2014 at 11:38 PM 0
Share

I think I haven't made myself very clear, sorry for that... The game I'm making is event based, so it isn't real time. Time only passes when certain buttons are pressed.

avatar image superluigi · Mar 15, 2014 at 02:27 AM 0
Share

ahh ok. That makes it so much simpler.

avatar image grimreaperpt · Mar 15, 2014 at 11:51 AM 0
Share

First of all, thank you for your help :)

The problem with your solution is that I would have to manually define everytime when the player to be hungry, and if I advance the game for, let's say, 8 hours, the hunger doesn't update (in that example it just updates when 2 or 4 hours have passed after eating, and the hour must be exactly that specified)

I have come up with another solution but it also doesn't work:

 //this is the code just for the sleep function
 var sleepHours = 8;
 
 function playerSleep () {
 
 var saveCurrentHour = hour + sleepHours;
 
 for (var i=hour  ; i<=saveCurrentHour ; i++)
 {
 hour = i;
 }
 }

The time does pass, and this time 1 hour at a time when I click the button, which I thought it would be compatible with my code above. The problem is that Unity doesn't assume every hour (maybe it goes too fast, so the Update function doesn't have time to recognize the entire loop?) so when 8 hours pass, it only subtracts hunger 1 time (ins$$anonymous$$d of 4). I tried adding a yield to this function above but I get an error: "ArgumentException: Couldn't bind to method 'playerSleep'"

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

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

Related Questions

How often is the update function called ? 1 Answer

Delaying a dynamic Variable(transform.position for Ex.) 2 Answers

Played time? 2 Answers

Random time interval 1 Answer

timed value increase? 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