- Home /
Having problems with my timer
Hey i need help i have made a timer and it works but i want it to activate when its in a level not from the very beginning of the game cos when its a 3 mins timer but when i built the game and went though to the level though my menu it was 2:30mins cos it was playing though the time i was on the menu please help
here is my script
var startTime : float;
var timeRemaining : float;
function Start()
{
startTime = 180.0;
guiText.material.color = Color.red;
}
function Update () {
Countdown();
}
function Countdown(){
timeRemaining = startTime - Time.time;
if(timeRemaining > 0){
ShowTime();
}
ShowTime();
if(timeRemaining < 0) {
timeRemaining = 0;
TimeIsUp();
Debug.Log("Time remaining = " + timeRemaining);
}
}
function ShowTime(){
var minutes: int;
var seconds: int;
var timeString : String;
minutes = timeRemaining / 60;
seconds = timeRemaining % 60;
timeString = minutes.ToString() + ":" + seconds.ToString("D2");
guiText.text = timeString;
}
function TimeIsUp(){
Debug.Log("Time Is Up");
}
Thanks
Please refactor your script code - it is unreadable.
You could leave the GameObject inactive until the game starts and then activate it. If your time is in a different scene you could reset it when the scene loads - OnLevelWasLoaded.
I just formatted your code, please do this in future.
As Bovine said, disable the call to Countdown() in the update until the menu has been cleared e.g. create a boolean, set it to false. When the menu has been closed, set the boolean to true. In update, check if boolean is true, then Countdown();
But I dont know how to make a boolean please help i really cant do it.
This post will help then:
http://answers.unity3d.com/questions/26975/how-to-create-boolean-varibles-.html
You might note that I didn't know either (being a C# bod) but I took 30 seconds to surf the web for an answer - you might want to try doing that yourself.
here's a little demo of a boolean in action. Attach this script to the camera in a new empty scene. Click the left mouse button , then the right, etc. You can see how a boolean works.
#pragma strict
var myBool : boolean = false;
function Update()
{
if ( Input.Get$$anonymous$$ouseButtonDown(0) )
{
myBool = true;
}
if ( Input.Get$$anonymous$$ouseButtonDown(1) )
{
myBool = false;
}
}
function OnGUI()
{
if ( myBool == true ) // this is usually written as : if ( myBool )
{
GUI.Box(Rect(10, 10, 200, 25), "myBool is TRUE");
}
else if ( myBool == false ) // this is usually written as : if ( !myBool ) : note the !
{
GUI.Box(Rect(10, 10, 200, 25), "myBool is FALSE");
}
}
Using this and all the help in these comments, you should be able to work out how to use a boolean in your update before Countdown();
Answer by james_170482 · Jul 23, 2012 at 06:47 PM
could'nt you add the script to an empty gameobject in the scene, so it will only start when the scene is loaded?
No, the problem is the scene is starting, but the countdown is NOT to start until the menu has been viewed and dismissed.
But you could do this, so you return to the meny by loading a different scene and start the game by loading a level of your game. But you have some fundamentals to understand I think.
Thanks guys ^_^ it works thanks for all your help sorry if ive been a bother ^^
true and fair enough, I did not consider transitioning from menu to new scene to start timer.
(look at all these comments!)
@dalekandjedi what we were all pushing for is do you understand the idea of a boolean and how to use it. How about you submit an answer with your results, then you can accept it. If it's right, I'll give you a thumb Up for effort =]
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
qrest system 1 Answer
Help with movement script 2 Answers
Need Help Understanding Script! (Javascript) 1 Answer
i need some code suggestions 1 Answer