Script Help

I am faced with yet another problem. My scenario is this:

I am doing a guess the difference game so i have a count up timer that records the time taken. My problem is that when the user presses on a wrong part of the picture to increase his time by an amount.

For example here is my script:

var startTime:int =0;
var x1 : int = 0;
function Update () 
{
timeTaken= startTime + Time.time;

guiText.text = FormatTime (timeTaken);


}

function FormatTime (time)
{

     var intTime : int = time;
     var minutes : int = intTime / 60;
     var seconds : int = intTime % 60;
     var fraction : int = time * 10;
     fraction = fraction % 10;
     
     //Build string with format
     // 17[minutes]:21[seconds]:05[fraction]
     
     timeText = seconds.ToString ();
     timeText += ":" + fraction.ToString ();
     return timeText;
}

what should be static so that I could increase my time from another script and how do i increase the time by 1 second in the counter for example! thanks!

thank you very much!

timeTaken= startTime + Time.time;
guiText.text = FormatTime (timeTaken);

The timeTaken variable holds players current total time but it is not defined anywhere am I right?

I think it should give an error about that.

if you define that timeTaken as

static var timeTaken : int;

than you should be able to reach players current time from everywhere like this.

YourScriptName.timeTaken

nope actually it is not showing an error but thanks anyways

It’s not showing an error because Unityscript is being “”““helpful””“” and defining it for you (as a local variable, oh joy).

Ahaykal’s solution is definitely what you need. :slight_smile:

TimeThing.js

static var timeTaken : float = 0.0;

function Update() {
 timeTaken += Time.deltaTime;
}
ClickHandler.js

 if ( badClick ) {
  TimeThing.timeTaken += 2;
 }
}

That is actually great thanks for answering that point Vincenti. Sometimes I get tired to defining variable :slight_smile:

I guess you meant oq4n’s solution is what you need :slight_smile:

Thanks for both of you but i guess I have found another way! thanks for your guidance again!

My brain! x_x
Yes, oq4n’s solution, lol.