my code stock.com

Danilo Ulf Mattick

Time Counting System
by Danilo Ulf Mattick

With this code you will build a counter for hours, minutes and secounds. Execute in the "Update Function" and when nessaccary update text in canvas.

Snippet options

Download: Download snippet as time-counting-system.cpp.
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!

    [Header("Time Counting")]
    private float _isSecound = 0f;
	private float _isMinute = 0f;
	private float _isHour = 0f;
	private float _timeSubstract;
	
	void Start() {
	    
	    //time - 60 Secounds to start form zero
		_timeSubstract = 60f;
	    
	}
	
	void Update() {
	    
	    TimeCounter();
	    
	}
	
	
	private void TimeCounter () {
		//strings 
		string minNull = "0";
		string hourNull = "0";
		string secNull = "0";

		//current values of time for every game start reseted
		_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"; //update canvas text variables

		if (_gover == true) { //when game over
			_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.