Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KelseySHock · Jun 24, 2018 at 06:07 PM · datetime

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;
 }

}

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 ShadyProductions · Jun 24, 2018 at 06:42 PM 0
Share

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.

avatar image KelseySHock · Jun 24, 2018 at 06:52 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
         }
     }
Comment
Add comment · Show 1 · 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 KelseySHock · Jun 25, 2018 at 04:12 PM 1
Share

Thank you! This fixed everything!!

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

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

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


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