my code stock.com

Danilo Ulf Mattick

Time Counting Method for Unity 2017
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.

Snippet options

Download: Download snippet as time-counting-method-for-unity-2017.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; }
		}
			
		if (secound >= 60f) {
			_isMinute++;
		} else if (_isSecound >= 60f && _isMinute == 0f) {
			_isMinute = 1;	
		}

		if (_isMinute >= 60f) {
			_isHour++;
			_isMinute = 0;
		}

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

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

		if (_isMinute >= 10f) {
			minNull = "";
		}

		if (_isHour >= 10f) {
			hourNull = "";
		}

		if (secound >= 10f) {
			secNull = "";
		}

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

		if (_gover == true) {
			_isSecound = 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.