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