-1

I am using Unity to create a 2D rpg game and i create a clock in the game to show the time and day and i want to sync the time in different scene as when i go into the other scene , the clock and day will return to 0 again, so i want to keep the time synchronized.

Here the code for the clock(I have tried the playerpref.)


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

public class ClockUI : MonoBehaviour {

    private const float REAL_SECONDS_PER_INGAME_DAY = 60f;

    private Transform clockHourHandTransform;
    private Transform clockMinuteHandTransform;
    public Text dayText;
    public Text timeText;
    public int dayString;
    private float day;
    public int DaySaved;

    private void Awake() {
        clockHourHandTransform = transform.Find("hourHand");
        clockMinuteHandTransform = transform.Find("minuteHand");
        timeText = transform.Find("timeText").GetComponent<Text>();
        dayText = transform.Find("dayText").GetComponent<Text>();
    }

    private void Update() {
        DaySaved = PlayerPrefs.GetInt("Day");

        day += Time.deltaTime / REAL_SECONDS_PER_INGAME_DAY;


        float dayNormalized = day % 1f;


        float rotationDegreesPerDay = 360f;
        clockHourHandTransform.eulerAngles = new Vector3(0, 0, -dayNormalized * rotationDegreesPerDay);

        float hoursPerDay = 24f;
        clockMinuteHandTransform.eulerAngles = new Vector3(0, 0, -dayNormalized * rotationDegreesPerDay * hoursPerDay);

        string hoursString = Mathf.Floor(dayNormalized * hoursPerDay).ToString("00");

        float minutesPerHour = 60f;
        string minutesString = Mathf.Floor(((dayNormalized * hoursPerDay) % 1f) * minutesPerHour).ToString("00");


        dayString = (int)Mathf.Floor(day);
        PlayerPrefs.SetInt("Day", dayString);


        timeText.text = hoursString + ":" + minutesString;
        dayText.text = "Day: " + DaySaved;

        PlayerPrefs.SetInt("Day", dayString);

    }


}

(I have tried the playerpref and it didnt work

MK K
  • 1
  • 1
  • Does this answer your question? [How to pass data (and references) between scenes in Unity](https://stackoverflow.com/questions/32306704/how-to-pass-data-and-references-between-scenes-in-unity) – shingo May 05 '23 at 11:37
  • No i tried but it didnt work – MK K May 05 '23 at 11:48
  • Is it possible to abstract the property of the clock into a storage system? SQLite, Cache, Radis...? – Rikudou En Sof May 05 '23 at 11:58
  • The top answer shows 3 methods, have you tried a different one? And [”it didnt work“ is not helpful](http://idownvotedbecau.se/itsnotworking/) – shingo May 05 '23 at 12:08

2 Answers2

0

to use: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

or if you want to do it manually, save the current date and time in a method (it can be Json), and load the new scene in the same script, except that in void start it checks if there is any saved time, if it exists it loads the time and current date, and then erases the Json data. That way, whenever it passes from scene, it will save from a script and load at the start of the other.

-1

How about creating a singleton class for your ClockUI and using it in your scenes. As this will always be the same object, the time should keep running.

Something like this:

public sealed class ClockUI : MonoBehaviour
{
    public static ClockUI Instance { get; private set; }
    private void Awake() 
    { 
        // If there is an instance, and it's not me, delete myself.
        if (Instance != null && Instance != this) 
        { 
            Destroy(this); 
        } 
        else 
        { 
            Instance = this; 
        } 
         
        /* Rest of Awake Method */
    }

    /* Rest of your code */
}

And then, instead of instanciating a new Instance every time you just get your single instance

var yourClock = ClockUI.Instance;
Xavjer
  • 8,838
  • 2
  • 22
  • 42
  • Even though advice can be counted as useful, it is incorrect. In Unity3d classes that are derived from the MonoBehaviour, cannot have a constructor as they have their own lifecycle. – Morion May 05 '23 at 12:23
  • I did not use Unity for some time and forgot this. You are of course right with the constructor, but the Singleton pattern exists and would be a valid way for this to work (Maybe https://gamedevbeginner.com/singletons-in-unity-the-right-way/ will be a better example here) – Xavjer May 05 '23 at 12:31