Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Nov 05, 2016 at 06:29 PM · timesystemboolrealtimedaycycle

What's The Best Way to Change 3 Bools For Real Time Morning, Afternoon And Night

I'm looking for a fairly simple method of changing 3 bools Using real system time in C#:

public bool isMorning = false; public bool isAfternoon = false; public bool isEvening = false;

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by NoseKills · Nov 05, 2016 at 07:35 PM

If it can only be Morning, Afternoon or Night (3 states) at any given moment, you should not use 3 booleans (they can represent 8 states). You can avoid bugs such as all 3 booleans becoming true just by choosing your variables in way that prevents it.

 using System;
 using UnityEngine;
 
 public enum TimeOfDay {
     Morning,
     Afternoon,
     Night,
 }
 
 public class TimeOfDayStart {
     public int StartHour;
     public TimeOfDay Name;
 
     public TimeOfDayStart(int startHour, TimeOfDay name) {
         StartHour = startHour;
         Name = name;
     }
 }
 
 public class MyScript : MonoBehaviour {
 
     public TimeOfDay currentTimeOfDay;
     public static readonly TimeOfDayStart[] TimeOfDayStartHours = {
             new TimeOfDayStart(4, TimeOfDay.Morning),
             new TimeOfDayStart(12, TimeOfDay.Afternoon),
             new TimeOfDayStart(20, TimeOfDay.Night),
         };
 
     void Update () {
         var now = DateTime.Now;
         currentTimeOfDay = TimeOfDay.Night;
         foreach (var tod in TimeOfDayStartHours) {
             if (now.Hour >= tod.StartHour) {
                 currentTimeOfDay = tod.Name;
             }
         }
         Debug.Log(currentTimeOfDay);
     }
 }
Comment
Add comment · Show 4 · 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 KnightRiderGuy · Nov 05, 2016 at 08:05 PM 0
Share

Thanks @Nose$$anonymous$$ills, this looks to be exactly what I need, but how would I access the different times of day from another script?

avatar image Owen-Reynolds · Nov 05, 2016 at 08:35 PM 0
Share

3 bool is 8 different combinations (you wrote 6.)

avatar image NoseKills Owen-Reynolds · Nov 07, 2016 at 07:51 AM 0
Share

Oopsy. Editing...

avatar image KnightRiderGuy NoseKills · Nov 07, 2016 at 04:12 PM 0
Share

Thanks @Nose$$anonymous$$ills, This worked great.... say if I wanted to add in a few other parameters like a check for: Breakfast Time Lunch Time Supper time

How would be best to further sub divide this but yet still have the ability to distinguish between: $$anonymous$$orning Afternoon Nighttime

avatar image
0

Answer by KnightRiderGuy · Nov 08, 2016 at 01:55 PM

Does anybody know of a good way to further subdivide this method to allow for being able to tell if it is either:

  • Breakfast Time

  • Lunch Time

  • Dinner time

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

59 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

Related Questions

Counting Days Using In-Game Time System? 1 Answer

How to change day & time in 12hr that moves when pressing a button 0 Answers

Get device time type(24h or am/pm) 0 Answers

Get Time A Bool Has Been True 4 Answers

System.Action has some invalid arguments 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