简体   繁体   中英

How I can make "Time" to be update constantly

I am beginner game dev in unity and i start new little project "alone", and in my game i have a little problem.

My problem is that the time, ie the clock in the game is not synchronized and does not synchronize and I have some suspicions.

I know maybe I'm doing another wrong step, maybe I put a wrong equation in the whole function and nothing works anymore, but to tell you more briefly, I want to make a game in which the game interface is a desktop and the clock on I coded it below, it doesn't work to be updated with time, I waited and tried many solutions that I applied and they didn't work, so I'll leave the code below

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class DateTime : MonoBehaviour
{

    public Text timeLockScreenText;
    public Text dateLockScreenText;
    public Text timeTaskBarText;
    string time = System.DateTime.UtcNow.ToLocalTime().ToString("HH:mm");
    string date = System.DateTime.UtcNow.ToLocalTime().ToString("MM-dd-yyyy");

    // Update is called once per frame
    void Update()
    {
        InvokeRepeating("DateTimeV",0f,1f);
    }

    public void DateTimeV()
    {
    timeLockScreenText.text = time;
    dateLockScreenText.text = date;
    timeTaskBarText.text = timeLockScreenText.text;
    }
}

Now I have some theories:

  • one is that in the game, just like in real life, it's like with any personal computer user when we open it there is a lockscreen with a clock and we press enter to enter all the time to access the internet and other files on the desktop, so I have the theory that the lockscreen being a GameObject which is like visibility opposite to the desktop, ie if the lockscreen is active the desktop is not visible and vice versa, the clock does not update because let's say if we are on the desktop the lockscreen is disabled and that's why the script doesn't really work and you have to to make two separate ones, one for the lockscreen and one for the desktop, actually for the clock on the desktop
  • or I don't know how to code well

I tried to put in Update() function, in InvokeRepeting, StartCourutine with IEnumerator function, and i dont have the solutions

I don't know anything about Unity, but it seems that you're never updating the values of time and date after they're initially assigned. Perhaps something like this would do the trick, where you just assign the correct values in the method:

public void DateTimeV()
{
    DateTime utcNow = System.DateTime.UtcNow.ToLocalTime();

    timeLockScreenText.text = utcNow.ToString("HH:mm");
    dateLockScreenText.text = utcNow.ToString("MM-dd-yyyy");
    timeTaskBarText.text = timeLockScreenText.text;
}

Use fixed update, and the code from Rufus L :

void FixedUpdate() => DateTimeV();

public void DateTimeV()
{
    DateTime utcNow = System.DateTime.UtcNow.ToLocalTime();

    timeLockScreenText.text = utcNow.ToString("HH:mm");
    dateLockScreenText.text = utcNow.ToString("MM-dd-yyyy");
    timeTaskBarText.text = timeLockScreenText.text;
}

What Rufus L said + you would use InvokeRepeating only once eg in

private void Start()
{
    InvokeRepeating(nameof(DateTimeV), 0f, 1f);
}

or use a simple counter

private float timer;

private void Update ()
{
    timer += Time.deltaTime;

    if(timer > 1f)
    {
        timer -= 1f;

        DateTimeV();
    }
}

or a coroutine

private IEnumerator Start()
{
    while(true)
    {
        DateTimeV();

        yield return new WaitForSeconds(1f);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM