- Home /
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");
}
}
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.
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.
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
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.
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
Follow this Question
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