Timer in Unity Game

I’ve coded a simple timer in unity using objects.
See this Gyazo:
nieuweversie - GameSceneTwoPlayers - PC, Mac & Linux Standalone - Unity 2020.3.27f1 Personal* (gyazo.com)

Is there a better way to to this, I am feeling I used to much code for this.

public class TimerButton : MonoBehaviour
{
    public GameObject ButtonOne;
    public GameObject ButtonTwo;
    public GameObject ButtonThree;
    public GameObject ButtonFour;
    public GameObject Questions;
    public GameObject Border;
    public GameObject PlayerOne;
    public GameObject PlayerTwo;

    public GameObject HitMe;
    public GameObject One;
    public GameObject Two;
    public GameObject Three;

    public AudioSource SourceEndTimer;
    public AudioClip ClipEndTimer;

    public AudioSource SourceTimerNumber;
    public AudioClip ClipTimerNumber;

    public void Start()
    {
        DisableGame();
    }

    public void ButtonClick()
    {
       
        StartCoroutine(ButtonClickStart());
    }

    IEnumerator ButtonClickStart()
    {
        HitMe.SetActive(false);
        SourceTimerNumber.PlayOneShot(ClipTimerNumber);
        Questions.SetActive(true);
        yield return new WaitForSeconds(1);

        Three.SetActive(false);
        SourceTimerNumber.PlayOneShot(ClipTimerNumber);
        Two.SetActive(true);

        yield return new WaitForSeconds(1);

        Two.SetActive(false);
        SourceTimerNumber.PlayOneShot(ClipTimerNumber);
        One.SetActive(true);

        yield return new WaitForSeconds(1);

        One.SetActive(false);
        EnableGame();
        SourceEndTimer.PlayOneShot(ClipEndTimer);

    }
   
    void DisableGame()
    {
        ButtonOne.SetActive(false);
        ButtonTwo.SetActive(false);
        ButtonThree.SetActive(false);
        ButtonFour.SetActive(false);
        Questions.SetActive(false);
        Border.SetActive(false);
        PlayerOne.SetActive(false);
        PlayerTwo.SetActive(false);
        One.SetActive(false);
        Two.SetActive(false);
    }

    void EnableGame()
    {
        ButtonOne.SetActive(true);
        ButtonTwo.SetActive(true);
        ButtonThree.SetActive(true);
        ButtonFour.SetActive(true);
        Questions.SetActive(true); 
        Border.SetActive(true);
        PlayerOne.SetActive(true);
        PlayerTwo.SetActive(true);
    }
}

For the code, arrays and loops would help. As an example for part of this code:

public GameObject[] Numbers; //set in inspector

IEnumerator ButtonClickStart() {
...
foreach (var thisNumber in Numbers) {
   thisNumber.SetActive(true);
   SourceTimerNumber.PlayOneShot(ClipTimerNumber);
   yield return new WatiForSeconds(1);
   thisNumber.SetActive(false);
}
...
}

Can do the same for buttons, etc.

That said, you could easily put all this into an Animator and not have to code it at all.

2 Likes

Thanks

This is the new code may someone ever need it.

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

public class TimerButton : MonoBehaviour
{
    public GameObject HitMe;

    public AudioSource SourceEndTimer;
    public AudioClip ClipEndTimer;

    public AudioSource SourceTimerNumber;
    public AudioClip ClipTimerNumber;

    public GameObject[] Numbers; //set in inspector
    public GameObject[] AllGameObjects; //set in inspector

    public void Start()
    {
        foreach (var thisGameObject in AllGameObjects)
        {
            thisGameObject.SetActive(false);
        }
    }

    public void ButtonClick()
    {
        StartCoroutine(ButtonClickStart());
        HitMe.SetActive(false);
    }

    IEnumerator ButtonClickStart()
    {
         foreach (var thisNumber in Numbers)
         {
            thisNumber.SetActive(true);
            SourceTimerNumber.PlayOneShot(ClipTimerNumber);
            yield return new WaitForSeconds(1);
            thisNumber.SetActive(false);
        }

        foreach (var thisGameObject in AllGameObjects)
        {
            thisGameObject.SetActive(true);
        }
    }
}