- Home /
Add seconds to timer countdown.
Okay so I have a pretty basic countdown timer here that counts down x # of seconds. It works to countdown:). The problem is I want to add x amount of seconds to the time when you hit a gameobject tagged timed. The problem is I get errors so something is not right. (I am still new to scripting)
private var startTime; private var restSeconds : int; private var roundedRestSeconds : int; private var displaySeconds : int; private var timed = false; private var displayMinutes : int; var countDownSeconds : int; function Awake() { startTime = Time.time; } function OnGUI () { // I need to make sure that my time is based on when this script was first called //instead of when my game started var guiTime = Time.time - startTime; restSeconds = countDownSeconds - (guiTime); //display messages or whatever here -->do stuff based on your timer if (restSeconds == 60) { print ("One Minute Left"); } if (restSeconds == 0) { Application.LoadLevel(loadedLevel); //I may add more later } //display the timer roundedRestSeconds = Mathf.CeilToInt(restSeconds); displaySeconds = roundedRestSeconds % 60; displayMinutes = roundedRestSeconds / 60; text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); GUI.Label (Rect (400, 25, 100, 30), text); }
function OnControllerColliderHit(Hit : ControllerColliderHit) { if(Hit.gameObject.tag == "fallout2") { timed = true; } }
function LateUpdate() { if(timed) { restSeconds == 60; timed = false; Destroy(OnControllerColliderHit.gameObject); } }
That is my code. As seen above I also have a problem with resetting the level. It does not want to load. Man I feel stupid when it comes to scripting. Anyway thanks for your time. :)
I don't see an obvious error, but I'd replace display$$anonymous$$inutes = roundedRestSeconds / 60; with display$$anonymous$$inutes = (roundedRestSeconds-displaySeconds) / 60; for more exact results.
23,23 $$anonymous$$ identifier: 'loadedlevel' 46,33 'gameObject' is not a member of 'function(UnityEngine.ControllerColliderHit): void'.
Answer by Uzquiano · Apr 29, 2011 at 09:37 PM
Hi,
When you reset you should change this:
restSeconds = 60;
And about the LoadLevel error, you should define loadedLevel as :
var loadedLevel : int;
loadedLevel = 1;//for example 1
Take a look to this. If you want to load the scenes as strings, a string is always between "Helo", for example:
var loadedLevel : String;
loadedLevel = "NameoftheLevel";
You should change here, not inside the if() ;)
function LateUpdate()
{
if(timed)
{
restSeconds = 60;
timed = false;
Destroy(OnControllerColliderHit.gameObject);
}
}
I now have 7 errors 1. Assets/New Folder 2/timer.js(37,10): BCE0044: expecting (, found 'OnControllerColliderHit'. 2. Assets/New Folder 2/timer.js(37,38): BCE0044: expecting ), found ':'. 3. Assets/New Folder 2/timer.js(37,39): UCE0001: ';' expected. Insert a semicolon at the end. 4. Assets/New Folder 2/timer.js(37,61): BCE0043: Unexpected token: ). 5. Assets/New Folder 2/timer.js(39,1): BCE0043: Unexpected token: if. 6. Assets/New Folder 2/timer.js(39,37): UCE0001: ';' expected. Insert a semicolon at the end. 7. Assets/New Folder 2/timer.js(41,7): BCE0044: expecting :, found '='.
I chose to remove the Destroy the gameObject command and it removed the error. The code you did made the load level work. :) The problem is when I collide into the gameobject it does nothing. I have box colliders on both game object and I even tried as the gameObject box collider as ''is triggered'' but that does not seem to help. Any ideas?
Answer by sneftel · Apr 29, 2011 at 09:42 PM
That is some really complicated code for what you're trying to do. :-) Anyhoo, the basic problem here is that even though restSeconds appears to be a class variable, you overwrite its value in OnGUI, so it doesn't actually lead to any extra information being kept around.
Try this: remove startTime entirely, and make restSeconds a float. In Awake() (or, depending on what you want, in Reset()), set restSeconds to countDownSeconds. In OnGUI(), remove the guiTime variable and don't change restSeconds. (Also, for the one minute warning, use RoundedRestSeconds.) In Update() or LateUpdate(), decrement restSeconds by the value of Time.deltaTime. To increase the amount of time left, just add to restSeconds.
Answer by Chris 4 · May 02, 2011 at 10:45 PM
I figured it out! However I could not have done it without the help of Uzquiano so I am going to put him as the good answer. I changed the code so that when you hit the collider of the object tagged timed it resets the timer to 40 seconds (The starting time) Thank you all!:)
Your answer
Follow this Question
Related Questions
How To Reset Timer on a Collision (C#) 0 Answers
Timer reset after elapsed time. 1 Answer
Resetting Combos 1 Answer
The timer starts after a collision with an object 1 Answer
Why is this Reset code not working? 3 Answers