How to add a game score based on time?


How would you add a score, that is the time that the player has been spawned? so if the player has been alive for 5 seconds his score would be five seconds then end once he dies and restarts once they press play again?

Also how would you save this into a leaderboard only showing their top 10 or is that too advanced?

Thanks for the help,


What you need is: Time.

Also you can search for creating timers using it.

First, you might also want to read this: What have you tried?

add this method and put that line in Start() method of your player’s script
This is the simplest way you can do without using Timers. which again uses Update function to check Time.time which is again a pain.

** EDIT: check the updated answer

This is similar to coroutines. You can use them as well but this is a simplified use of coroutines

GameManager manager;

void Start()
   // Since you need the score to be available even after player dies:
   // Have the score variable in another script called GameManager.cs maybe
   // get the reference to the game manager at start so you wont need to access it
  // everytime
  // I would suggest you a singleton class for game manager but for now lets assume
  // that you have the GameManager.cs attached to main camera and it has a score
  // variable

   manager = Camera.main.GetComponent<GameManager>();

   // this will invoke the method 1 second from start and every 1 second repeating
   InvokeRepeating("increaseScore", 1, 1);


void increaseScore()
   manager.score ++;


public class GameManager : MonoBehaviour
  public int score = 0;

You then display this score using a GUIText maybe.