Search code examples
imageunity-game-enginetimersprite

How do I make timer change a sprite every second without using code that's redundant?


I'm not entirely sure how to phrase question so sorry if this is confusing. Anyways for context I'm making a sort of minesweeper type of game in unity and one of the things the original game had was a timer. Here it is. I want to copy that sort of thing, and while I do have code that works, it's honestly kind of redundant here's what I have .

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    public float timer = 0;
    public bool isStop = false;
    public Image scoreCount;
    public Sprite[] numbersprite;
    // Start is called before the first frame update
    void Start()
    {
        timer = 0;
        isStop = true;
    }

Ignore all the stuff on the top.

    // Update is called once per frame
    void Update()
    {
        if(!isStop)
        {
            timer += Time.deltaTime;
            if(timer >= 1f)
            {
                scoreCount.sprite = numbersprite[1];
            }
            if(timer >= 2f)
            {
                scoreCount.sprite = numbersprite[2];
            }
            if(timer >= 3f)
            {
                scoreCount.sprite = numbersprite[3];
            }
            if(timer >= 4f)
            {
                scoreCount.sprite = numbersprite[4];
            }
            if(timer >= 5f)
            {
                scoreCount.sprite = numbersprite[5];
            }
            if(timer >= 6f)
            {
                scoreCount.sprite = numbersprite[6];
            }
        }
    }
}

What I want is to make it so that it both displays a specific sprite after a certain amount of time has based but also not have to resort to using any of this. Is there any way I can make this work? If you want some more information I can give you that.


Solution

  • This code is the ultimate solution to the problem and can support sprite indefinitely. It is also fully optimized. Just put the sprites 0 to 9 in the first list and the images in the second list respectively.

    public Sprite[] spriteNumbers = new Sprite[10]; // fill with numbers
    
    public List<Image> spriteFieds; // set Images Based on a unit of tens of hundreds
    public void Start() => InvokeRepeating(nameof(SyncTimer), 0f, 1f);
    public void SyncTimer()
    {
        for (var i = 0; i < spriteFieds.Count; i++)
        {
            var index = (int) (Time.time / Mathf.Pow(10, i) % 10);
            spriteFieds[i].sprite = spriteNumbers[index];
        }
    }
    

    How to make Stop Timer?

    Here I created a standalone timer field and you can stop the step timer by pressing the Space key. You can also reset the timer with the R key, for example.

    public Sprite[] spriteNumbers = new Sprite[10]; // fill with numbers
    
    public List<Image> spriteFieds; // set timer Fields
    
    public bool isStop;
    public float timer;
    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) isStop = !isStop;
    
        if (Input.GetKeyDown(KeyCode.R))
        {
            timer = 0;
            SyncTimer();
        }
        if (!isStop)
        {
            timer += Time.deltaTime;
            SyncTimer();
        }
    }
    public void SyncTimer()
    {
        for (var i = 0; i < spriteFieds.Count; i++)
        {
            var index = (int) (timer / Mathf.Pow(10, i) % 10);
            spriteFieds[i].sprite = spriteNumbers[index];
        }
    }
    

    The Timer Result:

    enter image description here