my code stock.com

Danilo Ulf Mattick

Time Counting Method for Unity 5
by Danilo Ulf Mattick

This method count the elapsed time during running the game and splitting it in variables of _isHour, _isMinute, _isSecound. All variable with underscore at first will defined before void Start() with a value of 0 (private float _isSecound = 0f). _timeSubstract get its value within void Start() ... of "60f" to ensure that there are not problems appears.

I had tested it now and could also make sure that the seconds always start again at zero.

Snippet options

Download: Download snippet as time-counting-method-for-unity-5.c.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

private void TimeCounter () {

		//strings 
		string minNull = "0";
		string hourNull = "0";
		string secNull = "0";

		//current values of time
		_isSecound = Time.timeSinceLevelLoad; //true secounds
		float secound = _isSecound;

		//reset the value of secound every 60 sec to zero
		if (secound >= _timeSubstract) {
			secound = _isSecound - _timeSubstract;
			if (_isSecound >= _timeSubstract + 60f) { _timeSubstract += 60f; }
			//Debug.Log ("Value TIME SUBSTRACT: " + _timeSubstract);
		}

        int multi = 1;
        _isMinute = _isSecound / 60f * multi;
		_isHour = _isSecound / 3600f;
		
		if (_isMinute == 60) {
		    multi++;
		}
		

		if (_isHour >= 24f) {
			_gover = true;
		}

		_isSecound = Mathf.CeilToInt (_isSecound);
		_isMinute = Mathf.CeilToInt (_isMinute);
		_isHour = Mathf.CeilToInt (_isHour);
		secound = Mathf.CeilToInt (secound) -1;

		if (_isMinute > 9) {
			minNull = "";
		}

		if (_isHour > 9) {
			hourNull = "";
		}

		if (secound > 9) {
			secNull = "";
		}

		_TIMECOUNTER.text = "Survived Time " + hourNull + _isHour.ToString() + " h : " + minNull + _isMinute.ToString() + " min : " + secNull + secound + " s";

		if (_gover == true) {
			_STAYALIVE._scoreSaveAlive = _score;
			_STAYALIVE._timeHour = _isHour;
			_STAYALIVE._timeMinute = _isMinute;
			_STAYALIVE._timeSecound = secound;
		}

	} // end TimeCounter

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.