In Game Clock Won't Advance Time

Hello

My Issue:
My in game clock can tell time but when I want it to move time forward won’t do it. At 7:00 pm I want my character to go to sleep and wake up at 10:00 am but what is happening is that it won’t. I’ve used ints, and true/false statements and both don’t seen to work. When using an int to wait until 10:00 am it will never stop even if it says 10:00 am. With true/false statements it will work for 2-5 seconds but nothing will change.

public class GameClock : MonoBehaviour {

    public const int hoursInDay = 24, minutesInHour = 60;
    public float dayDuration = 600f;//24 hours of ingame time is 10 minutes real world time...
    float totalTime = 0;
    float currentTime = 0;
    public int WakeUpTime;//thi is for when you wake up from sleeping...
    //day night cycle light source...
    public DayNightManager DayNight;
    public Sleep mySleep;
    public bool WakeUP;
    public bool MorningTime;
    public bool AfternoonTime;
    //12 hour clock UI
    public TMP_Text digitalClock;

    //analog clock
    public GameObject clockFace;
    public Transform hourHand;
    public Transform minuteHand;
    const float hoursToDegeers = 360 / 12, minutesToDegrees = 360 / 60;

    void Start()
    {
        WakeUP = false;
    }
    void Update()
    {
        totalTime += Time.deltaTime;
        currentTime = totalTime % dayDuration;
        //Digital Clock
        digitalClock.text = Clock12Hour();
        //Analog Clock
        hourHand.rotation = Quaternion.Euler(0, 0, -GetHour() * hoursToDegeers);
        minuteHand.rotation = Quaternion.Euler(0, 0, -GetMinutes() * minutesToDegrees);
        DayToNight();
    }
    public float GetHour()
    {
        return currentTime * hoursInDay / dayDuration;
    }
    public float GetMinutes()
    {
        return (currentTime * hoursInDay * minutesInHour / dayDuration) % minutesInHour;
    }
    public string Clock12Hour()
    {
        int hour = Mathf.FloorToInt(GetHour());
        //hour = 11;//the starts at 11 o'clock...
        string abbreviation = "PM";
        MorningTime = false;
        AfternoonTime = true;

        if (hour >= 12)
        {
            abbreviation = "AM";
            hour -= 12;
            MorningTime = true;
            AfternoonTime = false;
        }

        if (hour == 0) hour = 12;

        return hour.ToString("00") + " : " + Mathf.FloorToInt(GetMinutes()).ToString("00") + " " + abbreviation;
    }
    void DayToNight()
    {
        int time_Hour = Mathf.FloorToInt(GetHour());
        int time_Minute = Mathf.FloorToInt(GetMinutes());
        if (time_Hour >= 6 && time_Minute >= 30 && AfternoonTime)//if time is greater or equales 1 o clock then change color of day to another
        {
            DayNight.NextColor = 1;
        }
        if (time_Hour >= 6 && time_Minute >= 45 && AfternoonTime)
        {
            DayNight.NextColor = 2;
        }
        if (time_Hour >= 7 && AfternoonTime)
        {
            DayNight.NextColor = 3;
            mySleep.SleepCollider.GetComponent<BoxCollider2D>().enabled = true;
        }
        if (time_Hour >= 7 && time_Minute >= 15 && AfternoonTime)
        {
            DayNight.NextColor = 4;
        }
        if (time_Hour >= 7 && time_Minute >= 30 && AfternoonTime)
        {
            DayNight.NextColor = 5;
        }
        if (time_Hour >= 7 && time_Minute >= 45 && AfternoonTime)
        {
            DayNight.NextColor = 6;
        }
        if (time_Hour >= 8 && AfternoonTime)
        {
            DayNight.NextColor = 7;
        }
        if (time_Hour >= 10 && MorningTime == true && AfternoonTime == false)
        {//this is to make the color normal again...
            DayNight.NextColor = 0;
            WakeUP = true;
        }
    }
}

The second script is used to control when player can wake up from sleeping and advances time forward.

public class Sleep : MonoBehaviour
{
    public Player myPlayer;
    public DayNightManager DayNight;
    public GameClock myClock;
    public GameObject SleepCollider;
    public TMP_Text CanNotSleep;
    public GameObject ImageFade;
    public bool CanGoToSleep;
    public bool CanClickYButton_Sleep;
    public bool NoSleepBool;
    public bool IsAwake;
    int hour;

    void Start()
    {
        hour = Mathf.FloorToInt(myClock.GetHour());
        CanNotSleep.enabled = false;
        CanGoToSleep = false;
    }
    void Update()
    {
        GoToSleep();
    }
    void GoToSleep()
    {
        if (CanGoToSleep == true && NoSleepBool == false)
        {
            StartCoroutine(SleepTime());
        }
    }
    IEnumerator SleepTime()
    {
        SleepCollider.GetComponent<BoxCollider2D>().enabled = false;
        CanNotSleep.enabled = false;
        CanClickYButton_Sleep = false;
        CanGoToSleep = false;
        NoSleepBool = true;
        myPlayer.speed = 0f;
        float endValue1 = 255;
        float endValue2 = 0;
        float duration = 5;
        float time = 0;
        float startValue = ImageFade.GetComponent<Image>().color.a;
        bool starFadeBool;
        starFadeBool = true;
        if (starFadeBool)
        {
            while (time < duration)
            {
                ImageFade.GetComponent<Image>().color = new Color(0f, 0f, 0f, Mathf.Lerp(startValue, endValue1, time / duration));
                time += Time.deltaTime;
            }
            ImageFade.GetComponent<Image>().color = new Color(0f, 0f, 0f, endValue1);
        }
        myClock.dayDuration = 21f;
        yield return new WaitUntil(() => hour >= 10 && myClock.WakeUP == true && myClock.MorningTime == true && myClock.AfternoonTime == false);
        Debug.Log("WAKE UP...");
        myClock.dayDuration = 600f;
        yield return new WaitForSeconds(1f);
        starFadeBool = false;
        if (!starFadeBool)
        {
            while (time < duration)
            {
                ImageFade.GetComponent<Image>().color = new Color(0f, 0f, 0f, Mathf.Lerp(endValue1, endValue2, time / duration));
                time += Time.deltaTime;
            }
            ImageFade.GetComponent<Image>().color = new Color(0f, 0f, 0f, endValue2);
        }
        myPlayer.speed = 0.002f;
        myClock.WakeUP = false;
        IsAwake = true;
        yield return new WaitForSeconds(2f);
        IsAwake = false;
        yield return null;
    }
}

I thank anyone who help out.
Thank You.

Your code has several issues. First and foremost you have two scripts which both reference each other. This is generally a bad design and usually an indication that it probably should be a single class or that it needs better abstraction / seperation.

Next problem is you have way too many boolean values and some don’t seem to be set anywhere, like CanGoToSleep and NoSleepBool. Both are only set to one value, so either this code does not work properly, or you have even more scripts involved which mess with the state of other scripts directly which is also bad design ^^.

The next issue which is most likely more related to your missing delay is that in your coroutine you have two while loops without any yield in it. That means those while loops would complete within the same frame, so it doesn’t “wait” at all. You have to yield in order for the game to actually continue running. When you want to wait one frame, you have to use yield return null;

Another issue is that you have this line in your Start method:

hour = Mathf.FloorToInt(myClock.GetHour());

This would call the GetHour method, get the current hour and cast it to an int value which is stored in the hour variable. This hour variable would never update after that. This is just an integer variable and it does not magically update itself somehow ^^. Since you use this variable in your WaitUntil closure, this would probably never finish when hour was less than 10 when Start was called. So your coroutine would get stuck at this point.

You also have a lot of redundancy which makes the whole scripts just way more complicated. Like MorningTime and AfternoonTime which are always the opposite of each other. Due to this fact it doesn’t make much sense to check both variables in your WaitUntil. Your WakeUP variable would be enough since that is only set to true when the same conditions are true as well.

What’s also weird is that your coroutine would change the dayduration and then set it back to hardcoded values. It would make more sense to have some kind of timescale or acceleration value. The way you calculate the current hour / minute would be completely messed up when you change the dayDuration. The time would suddenly jump to some other values and when you revert the dayDuration you’re back at the old time. Time would not pass faster since you keep your time in totalTime which would not be affected by your change.

Since this is all very messy, it’s not really clear what exact behaviour you want. In any case, those scripts would need some major overhaul.