WACK-A-MOLE QUEST: ‘Counting Up’ - Solutions

Quest: Wack-A-Mole Quest
Challenge: Counting Up

Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.

I didn’t use Unity’s Time class as suggested, I used the Stopwatch class:

public class Timer : MonoBehaviour
{
    Text timerText; 
    System.Diagnostics.Stopwatch stopwatch;
    
    void Start()
    {
        stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();        
        timerText = GetComponent<Text>();
    }

    void Update()
    {        
        timerText.text = $"{(int)stopwatch.Elapsed.TotalSeconds}";
    }
}
2 Likes

I got it to work like this:

Text timerText; 
    float elapsedTime;
    
    void Start()
    {
        timerText = GetComponent<Text>();
    }

    void Update()
    {
        elapsedTime = Time.time;
        float elapsedTimeRounded = Mathf.Round(elapsedTime);
        timerText.text = elapsedTimeRounded.ToString();
    }
1 Like
Text timerText; 
    float elapsedTime;
    
    void Start()
    {
        timerText = GetComponent<Text>();
    }

    void Update()
    {
        elapsedTime = Time.timeSinceLevelLoad;
        timerText.text = Mathf.Round(elapsedTime).ToString();
    }

    public void ResetTimer()
    {
        elapsedTime= 0;
    }
1 Like

I ended up converting elapsedTime to an integer.

public class Timer : MonoBehaviour
{
    Text timerText; 
    int elapsedTime;
    
    void Start()
    {
        timerText = GetComponent<Text>();
    }

    void Update()
    {
        elapsedTime = (int)Time.timeSinceLevelLoad;
        timerText.text = elapsedTime.ToString();
    }
}

1 Like
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    Text timerText; 
    private int seconds = 1;
    
    void Start()
    {
        timerText = GetComponent<Text>();
    }

    void Update()
    {
        if (Time.time > seconds)
        {
            timerText.text = seconds.ToString();
            seconds++;
        }
    }
1 Like

added FindObjectOfType<Timer>().ToggleActive(); to the Scorer.Update() and EnemyMover.Start() and then modified the timer script as follows

add bool gameHasStarted
add public void ToggleActive() { gameIsActive = !gameIsActive; }
add if(gameHasStarted) elapsedTime+=Time.deltatime in Update()
add timerText.text = Mathf.RoundToInt(elapsedTime).ToString() in Update()

ToString() give you this nice option to use without decimal point

using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    Text timerText; 
    float elapsedTime;
    
    void Start()
    {
        timerText = GetComponent<Text>();
    }

    void Update()
    {
        elapsedTime = Time.time;
        timerText.text = elapsedTime.ToString("0");
    }
}
1 Like
public class Timer : MonoBehaviour
{
    Text timerText;
    float elapsedTime;

    void Start()
    {
        timerText = GetComponent<Text>();
    }

    private void Update()
    {
        elapsedTime += Time.deltaTime;
        timerText.text = Mathf.RoundToInt(elapsedTime).ToString();
    }
}
1 Like

We were discussing this portion of the challenge…with the methods being called in update, couldnt there be an issue with the timer not keeping true time since update happens every frame…would it be better to have it in fixed update?

Privacy & Terms