1
\$\begingroup\$

I'm making a "survival" game, where the goal is to stay on a rotating board as long as possible. I want to add a score system based on how many seconds passed. For example, if you stay on the board for 5 seconds, your score should be 5.

How can I do this in Unity using JavaScript?

\$\endgroup\$
2
\$\begingroup\$

What you're looking for is the Time object. And Time.time in particular. Time.time contains the time in seconds since the game has started (or the level has been loaded).

So you want to know how many seconds elapsed since the player is on the board. Just save the Time.time value when you want to start counting and then, in the Update method, you can easily know how many seconds have elapsed.

function Start() {
  // for the example we start counting at Start
  _start_time = Time.time;
}

function Update() {
  // elapsed time is equal to the current Time.time 
  // minus its value when we started to count
  var elapsed = Time.time - _start_time;
  Debug.Log( elapsed + "s have elapsed.");
}

I hope it helps.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.