I'm trying to write a script that will add 30 seconds to a timer that is constantly counting down. This is my timer script that is attached to a GUI Text: Code (csharp): var startTime = 30.0; function Update () { //The time left for player to complete level timeLeft = startTime - Time.time; //Don't let the time left go below zero timeLeft = Mathf.Max (0, timeLeft); //Format the time nicely guiText.text = FormatTime (timeLeft); } //Format time like this //12(minutes):34(seconds).5(fraction) 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 //12(minutes):34(seconds).5(fraction) timeText = minutes.ToString () + ":"; timeText = timeText + seconds.ToString (); timeText += "." + fraction.ToString (); return timeText; } I would like 30 seconds to be added to the timer whenever the game object "acorn" is destroyed. This is the code attached to the acorn to destroy it when the player is within the trigger collider and presses 1: Code (csharp): var acorn : GameObject; var score : Score; function OnTriggerStay(col : Collider) { if (Input.GetKey("1")) { score.AddToScore (); Destroy (acorn.gameObject); } } I assume there is a line of code that can be added after "Destroy (acorn.gameObject);" to add time to the timer, but I've been unsuccessful so far. Any help is g reatly appreciated. Thanks, Brian
I'm sorry, I don't really understand what you mean. If I add "timer.startTime += 30.0;" below "Destroy (acorn.gameObject);" I get an error of Unknown identifier 'timer'. I'm quite new to Unity and scripting so the more detailed you can be the better.
Your script should look like this: Code (csharp): var acorn : GameObject; var score : Score; var timer : Timer; function OnTriggerStay(col : Collider) { if (Input.GetKey("1")) { score.AddToScore (); Destroy (acorn.gameObject); timer.startTime += 30.0; } } Be sure that the name of your timer script is Timer
im using a similar script but having problems and wondered if you could help. script is: var startTime = 30.0; var minutes : int = 0; var seconds : int = 20; var fraction : int = 0; function Update () { //The time left for player to complete level timeLeft = startTime - Time.time; //Don't let the time left go below zero timeLeft = Mathf.Max (0, timeLeft); //Format the time nicely guiText.text = FormatTime (timeLeft); } //Format time like this //12(minutes):34(seconds).5(fraction) 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 //12(minutes):34(seconds).5(fraction) timeText = minutes.ToString () + ":"; timeText = timeText + seconds.ToString (); timeText += "." + fraction.ToString (); return timeText; } if (timeLeft <= 0.0) { Application.LoadLevel ("FixTheBridge"); } i get the error unknown identifier 'timeLeft' any ideas?
"TimeLeft" is never declared, but you're using it in Update(). Add Code (csharp): var timeLeft = startTime; up near the top with your other variable declarations. It will declare the timeLeft variable and give it the same initial value as starTime; Also, please use the code tags on the forum when posting code; it really helps when reading code on the forum.
This was a great help...thanks! One question, how could i modify so that when an object was picked up it added the time?? (Sorry i am a nooobie) Thanks for your help!!
Noobie here as well. I'm trying the same thing as the OP but I get "Object reference not set to an instance of an object On_touch2.OnTriggerEnter () (at Assets/labU/Scripts/On_touch2.js:18 )" I have two scripts, one for the timer, which is called barClock: Code (csharp): function Awake () { DontDestroyOnLoad (this); clockFGMaxWidth = clockFG.width; } var clockIsPaused : boolean = false; static var startTime : float; //(in seconds) var timeRemaining : float; //(in seconds) var percent : float; //for bar clock var clockBG : Texture2D; //for bar clock var clockFG : Texture2D; //for bar clock var clockFGMaxWidth : float; //starting width of the FG bar for bar clock var nextLevel : int; function Start() { startTime = 600.0; //time for the whole game timeRemaining = startTime; } function Update() { if (!clockIsPaused) { // make sure the timer is not paused DoCountdown(); } } function DoCountdown() { timeRemaining -= Time.deltaTime; percent = timeRemaining/startTime * 100; //for bar clock if (timeRemaining < 0) { timeRemaining = 0; clockIsPaused = true; TimeIsUp(); } ShowTime(); } and one called On_touch2 that looks like this: Code (csharp): var score:GameObject; var barClock : barClock; function Start() { score = GameObject.Find("Score"); } function Update () { } function OnTriggerEnter() { transform.parent.gameObject.audio.Play(); Destroy(gameObject); ++scoreScript.score; barClock.timeRemaining +=200.0; } This is the last bit of my game I need help with, after that it's tidying up, refactoring the code and adding levels. Please help B
Where exactly do you reference barClock? I see you put a variable for it... but where do you assign it....
Not sure I understand? Not trying to be thick, I *am* thick I've used functions from other scripts before for my game by putting a var at the top of the script that wants to reference and then the function from the referenced script works (like so: ScriptA contains functionX. ScriptB sets a var of scriptA:ScriptA and then later calls a function from ScriptA by stating ScriptA.FunctionX) is that not right? B
BTW. This error doesn't stop the game running, it just pops an error each time you hit a coin. Not like usual where you can't hit play until you've corrected your grievous coding sin. B
That should say programming. I would suggest you go do some basic coding tutorials. Your error is un-related to Unity, and more specifically coding basics. The answer to your problem is initialtime needs to be a public variable. Im not sure you'll understand what I mean, but thats the problem.