Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In-game score counter that depends on time C#

I have a very simple game, I wanted to add a score to it, after that it started to lag a lot, here is my score counter code, I think the problem is in it

using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public Text scoreText;
    public int scr = 0;
    
    void Update(){
       System.Threading.Thread.Sleep(1000);
       scr = scr + 1;
       scoreText.text = scr.ToString(); 
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you want to execute Update() method each 1000ms, maybe better aproach is to create coroutine executing each 1000ms.

void Start(){
       StartCoroutine(SampleCoroutine());
}

IEnumerator SampleCoroutine()
{
       while(true){
            yield return new WaitForSeconds(1);
            scr = scr + 1;
            scoreText.text = scr.ToString();
       }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading