- Home /
Time based scoring
Hello I am working on a game, can anyone tell me how to calculate the score based on seconds? For instance if the player plays for 10 seconds the score will be 10; 1 second = 1 point;
Answer by Fabkins · Apr 22, 2012 at 01:33 PM
var startTime: float;
function GameStarts()
{
startTime=Time.time;
}
function CalculateScore(): int
{
return ( Time.time - startTime);
}
Because time is granular , if you wanted to do you could multiple the result by 10 and have a score that has fractions of a second.
I just used the code but it gives me 2 errors! Assets/Scripts/$$anonymous$$ainScene/ScoreGUI.js(10,14): BCE0044: expecting (, found 'CalclulateScore'. Assets/Scripts/$$anonymous$$ainScene/ScoreGUI.js(10,29): BCE0044: expecting EOF, found '('.
How do I fix them!
I'm not really that sure what @Fabkins is doing here ... for one, he seems to have mixed a bit of C# with JS! You could either calculate the score at the end of a playing session by calling this function:
function CalculateScore () : int {
return Time.time;
}
or you could increment as you are going if you want like so:
var score : int;
function Update () {
score = Time.time;
}
Pretty darn simple.
Sorry typo.
@$$anonymous$$leptomaniac, no you cant use Time.time on its own as its the time since the program started not the time since the game started.
So, typos asside, my code stands.
PS corrected typo. I was having a mad moment and mixing JS and C#.
Thanks a lot for that! But when it prints on the screen it even displays $$anonymous$$illi-seconds with it, is there a way just to display seconds?
Answer by getbiks · Aug 30, 2012 at 08:51 AM
It displays mili-second because u have assigned the score avriable as float.
var score : int;
now the scoring will in fixed value
what you can also do is :
var score : int
function Start()
{
score = 0;
}
function Update()
{
score = Time.timeSinceLevelLoad;
Debug.Log("score");
}