- Home /
Question by
HSMInteractive · Feb 04, 2013 at 03:25 AM ·
timerscorepausescore systemtime.time
Pause Time.time?
**Ok so I have a script where I have an int: score, and it's equal to Time.time 150. What I want to do is when my character hits the ground, the score pauses at the value it was at when it hit the ground.*
Here is my code:
#pragma strict
var score : int = 0;
var style1 : GUIStyle;
function Start () {
}
function Update () {
score = Time.time * 150;
}
function OnTriggerEnter(collisionInfo : Collider){
if(collisionInfo.gameObject.tag == "Player"){
Debug.Log("You failed!");
//Pause score at current value here
}
}
function OnGUI(){
GUI.Label(Rect(100,100,200,40), "" + score.ToString(), style1);
}
Comment
Never$$anonymous$$d! I solved it myself.
#pragma strict
var score : int = 0;
var style1 : GUIStyle;
var finScore : int = 0;
var finished : boolean = false;
function Start () {
}
function Update () {
score = Time.time * 150;
}
function OnTriggerEnter(collisionInfo : Collider){
if(collisionInfo.gameObject.tag == "Player"){
Debug.Log("You failed!");
// Have score pause ar current value
finScore = Time.time + score;
finished = true;
Destroy(collisionInfo.gameObject);
}
}
function OnGUI(){
if(finished == false){
GUI.Label(Rect(100,100,200,40), "" + score.ToString(), style1);
}else if(finished == true){
GUI.Label(Rect(100,100,200,40), "" + finScore.ToString(), style1);
}
}
Best Answer
Answer by Wolfram · Feb 04, 2013 at 03:36 AM
Note that Time.time contains the absolute time since the beginning of your game. So your player can't start over, without having an ever increasing initial score. What you want to do instead is using a relative time:
var startTime:float=0.0;
function Start(){
startTime=Time.time; // store current time. you could also move this to when the player jumps or whatever
}
// no Update computation necessary
function OnTriggerEnter(collisionInfo : Collider){
....
// compute actual score
finScore=(Time.time-startTime)*150;
...