Search code examples
c#unity-game-engineunity3d-gui

Unity3D scripting timeSinceLevelLoad variable not returning c#


I am designing my first game and I am trying to create a time variable with a 5 sec step (like 5 times slower than real time).

This is the where I have the GUI (pasting only relevant parts):

using UnityEngine;
using Assets.Code.Interfaces;
using Assets.Code.Scripts;
using Assets.Code.PowerPlants;
namespace Assets.Code.States

Debug.Log (TimeManager.gametime);
public void ShowIt()
    {

GUI.Box (new Rect (Screen.width - 650, 10, 100, 25), TimeManager.gametime.ToString() );  // GAME TIME HOURS
}

This is where my game time is calculated:

using System;
using UnityEngine;
namespace Assets.Code.Scripts
{
    public class TimeManager
    {
        public static int gametime;
        public TimeManager ()
        {
        gametime = (int)Time.timeSinceLevelLoad / 5;
        }
    }
}

I get no errors, but the value of gametime is always 0. This worked before but is not working anymore and I have no idea why. Any hints?


Solution

  • namespace Assets.Code.Scripts
    {
        public class TimeManager
        {
            public static int gametime 
            { 
               get { return (int)Time.timeSinceLevelLoad / 5;  }
            }
        }
    }
    

    The ctor is not useful in your case, just add a property that returns the value you need.