Search Unity

Help with Creating Day and Time Script

Discussion in 'Scripting' started by Autry44, Jul 15, 2019.

  1. Autry44

    Autry44

    Joined:
    May 21, 2018
    Posts:
    4
    Before Anyone Says look at tutorials and google, I have this is my last resort, I always do my research before I post on a Forum.

    In my survival game I'm trying to get my UI to have a Day and Time of Hours in 24 hour format and minutes time, but I do not want to use my systems time, I want to have a custom time where the Day starts at Day 1 and counts up by 1 after the time 24:59 hits 0:00 for midnight,and I want the Hours and time to say start at 06:30 being 6:30 in the morning instead everything starts at (Days:00) I've tried to change to say(Days: 01) and time from (Time: 00:00) to (Time: 06:30) when I do this the timer just goes nuts on what it wants to count. The Day and Time of Hours and minutes are on two separate text components, in reality I want a similar system to 7 days to die in some aspect of how the days and time scale is to be in-game time and not a real time that goes off your system and counts up by one, I do not understand code all that well and am trying to understand it if someone could help me create this system so I can set the startDay and startHours etc. Much appreciated everyone!

    This is what I have so far

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    using UnityEngine.UI;

    using TMPro;



    public class Time_Script : MonoBehaviour

    {

    [SerializeField]

    private TextMeshProUGUI timeText;

    [SerializeField]

    private TextMeshProUGUI dayText;



    private float gameRunTime;

    [SerializeField] private float gameTimeScale;



    private string ampm;

    private float minutes;

    private float hours;

    private int day;



    private float currentHours;

    private int currentDay;



    void Start()

    {

    ampm = "AM";

    }



    void Update()

    {

    gameRunTime = Time.time;



    if (Input.GetKey(KeyCode.A))

    {

    Time.timeScale = 2;

    }

    ConvertTime();



    gameTimeScale = Time.timeScale;



    // Testing purposes Only!!

    if (Input.GetKey(KeyCode.UpArrow))

    {

    Time.timeScale = 50.0f;

    }

    ConvertTime();



    }



    private void ConvertTime()

    {

    minutes = Mathf.Floor(gameRunTime % 60);

    hours = Mathf.Floor(gameRunTime / 60 % 24);

    day = Mathf.FloorToInt(gameRunTime) / 1440 % 31;



    Display();

    }



    private void Display()

    {

    timeText.text = hours.ToString("00") + ":" + minutes.ToString("00"); //+ " " + ampm; (Disabled AmPm)

    dayText.text = day.ToString("00");

    }



    public void Play()

    {

    Time.timeScale = 10;

    Debug.Log("Play Fired");

    }



    private void FastForward()

    {

    Time.timeScale = 100;

    Debug.Log("FastForward Fired");

    }

    }