Timer on Roll-A-Ball Tutorial

Hello All,

I’m brand new to Unity and just went through the roll-a-ball tutorial. Now i’m trying to stop the timer upon collecting the last cube.

I found this for a timer, which works great. I just don’t now how to make it stop upon collecting the last cube.

*side note - Bonus points for anyone who know how to code the top five scores and list them when the game ends.

Thanks All!

public class Timer : MonoBehaviour {

public Text timerText;
private float startTime;
private bool finished = false;

void Start ()
{
	startTime = Time.time;
}

	
void Update ()
{
	if(finished)
		return;
				
		float t = Time.time - startTime;

		string seconds = (t % 60).ToString("f4");

	timerText.text = seconds;
}
	
public void finish()
{
	finished = true;
	timerText.color = Color.yellow;
}

}

A more sophisticated approach would be to use coroutines

Coroutines lets you create pauses in your code executions and are more flexible than Update().
You can start a coroutine when you collect a cube and than stop it when you collect another one.

THANKS!!! The timer stops on collecting the last cube. I’ll have to wait to try the leaderboard, when I have more time. Thanks again!!

1. In the PlayerController script, declare a Timer variable

public Timer Timer ;

2. In the editor, drag & drop the gameObject holding the Timer script into the Timer field of the PlayerController script.

3. In the SetCountText function, call the finish function of the Timer

void SetCountText ()
{
    countText.text = "Count: " + count.ToString ();
    if (count >= 12)
    {
        winText.text = "You Win!";
        Timer.finish();
    }
}

About your leaderboard, while I am not a big fan of the following solution, it may be the simplest one.

  1. Create a UI with your leaderboard and Texts as children of the leader board.

  2. Attach the script below to a new empty gameObject in your scene

  3. Drag & drop the Texts representing the best scores. Make sure to add them in the correct order (first one = best score ; last one = worst score). Don’t forget to add the leaderboard itself in the Leaderboard field.

  4. In the Time script, declare a public variable called HighScoresManager and call HighScoresManager.AddScore( Time.time - startTime ) ; and HighScoresManager.ShowLeaderBoard() in the finish function.

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

    public class HighScoresManager : MonoBehaviour
    {
    public GameObject Leaderboard;
    public Text HighScoresTexts;

    private void Start()
    {
        Leaderboard.SetActive(false);
    }
    
    public void ShowLeaderBoard( )
    {
        Leaderboard.SetActive(true);
    }
    
    public void AddScore( float score )
    {
        // Retrieve the highscores
        string serializedHighScores = PlayerPrefs.GetString("highscores");
        List<string> highScores = new List<string>( serializedHighScores.Split( new char[]{'-'}, StringSplitOptions.RemoveEmptyEntries) );
        
        // Add new score
        highScores.Add( score.ToString("f4") ) ;
        
        // Sort the scores in the descending order
        highScores.Sort( (s1, s2) => float.Parse( s2 ).CompareTo( float.Parse( s1 ) ) ) ;
        
        // Remove all the lowest scores
        if( highScores.Count > HighScoresTexts.Length )
            highScores.RemoveRange( HighScoresTexts.Length, highScores.Count - HighScoresTexts.Length);
        
        for( int i = 0 ; i < HighScoresTexts.Length && i < highScores.Count ; ++i )
            HighScoresTexts_.text = highScores*;*_
    

PlayerPrefs.SetString( “highscores”, String.Join(“-”, highScores.ToArray()) );
}
}