- Home /
How to Permenantly Change a Variable in Update()
I've hit a roadblock in my project. When my character is sleepy (ie. has 300 sleep points out of 1000) and goes to sleep the time goes forward like I want. However, when the character gets sleepy again after that and goes to sleep, instead of adding the hours to the NEW time since his first sleep, the last sleep's hours are taken off and the new sleep time's hours are added.
So my question is how can I make gameTimeHour permenantly change in Update()?
public class Clock : MonoBehaviour { Text text; DateTime currentTime; Sleep sleep;
// Use this for initialization
void Start () {
text = FindObjectOfType<Text>();
sleep = FindObjectOfType<Sleep>();
}
// Update is called once per frame
void Update ()
{
numTimesGettingASkill = skills.numSkillTimes;
currentTime = DateTime.Now;
int gameTimeHour = currentTime.Hour;
int gameTimeMinutes = currentTime.Minute;
int gameTimeSeconds = currentTime.Second;
if (sleep.goingToSleep == true)
{
if(sleep.hoursNeededToSleep >= 1)
{
gameTimeHour += Mathf.RoundToInt(sleep.hoursNeededToSleep);
}
else if (sleep.hoursNeededToSleep < 1 && sleep.hoursNeededToSleep > 0)
{
gameTimeHour += 1;
}
else
{
return;
}
}
text.text = gameTimeHour + ":" + gameTimeMinutes + ":" + gameTimeSeconds;
}
}
You're gonna have to give some more details like what is 'currentTime' variable etc, Also it would be nice to actually give an example of how the date uses the last date because it's not really clear to me.
So if 'goingToSleep' == true and say the time is 12:00 and and the hours needed to sleep is 2, then the time changes to 14:00. BUT if 'goingToSleep' == true again after that and say the hours needed to sleep is 1, then the time changes to 13:00 ins$$anonymous$$d of changing to 15:00 which is what I want.
Answer by ShadyProductions · Jun 25, 2018 at 06:46 AM
Well you're using DateTime.Now as your currentTime, Which you will effectively need to play one hour for it to even go up by an hour which makes sense.
What you need to do is put currentTime = DateTime.Now;
into Start method and update it by yourself in the Update() method. You will need to write your own logic when to add to seconds, minutes and hours.
Try this:
public class Clock : MonoBehaviour
{
Text text;
private DateTime currentTime;
Sleep sleep;
private float time;
// Use this for initialization
void Start()
{
text = FindObjectOfType<Text>();
sleep = FindObjectOfType<Sleep>();
currentTime = DateTime.Now;
}
// Update is called once per frame
void Update()
{
// Do time calculation first
time += Time.deltaTime;
if (time >= 1f)
{
currentTime = currentTime.AddSeconds(1);
time = 0f;
}
numTimesGettingASkill = skills.numSkillTimes;
if (sleep.goingToSleep == true)
{
if (sleep.hoursNeededToSleep >= 1)
{
currentTime = currentTime.AddHours(Mathf.RoundToInt(sleep.hoursNeededToSleep));
}
else if (sleep.hoursNeededToSleep < 1 && sleep.hoursNeededToSleep > 0)
{
currentTime = currentTime.AddHours(1);
}
else
{
return;
}
}
text.text = currentTime.Hour + ":" + currentTime.Minute + ":" + currentTime.Second;
}
}
Your answer
Follow this Question
Related Questions
C# DateTime inspector 2 Answers
Help, getting ambiguous reference error 1 Answer
How to save DateTime in a list 1 Answer
Check How Many Minutes Have Passed 1 Answer
How do I return the correct number of days between TimeSpans? 0 Answers