- Home /
Timer issue - bool value not setting timer to false?
Gday all,
I have got a problem with my timer - I want it to keep track of the time it takes from start to finish. I have got a good timer code working thanks to answers from other questions here but I have run into a hurdle trying to stop it.
I have made a maze game that creates levels from scripts rather than constructing it by manually placing the walls down. Initially I just have a Terrain and an empty Game Object called Startup with scripts attached. The attached scripts are Maze Load (Which loads the maze from an array and places the player at start location, exit trigger at the exit and walls etc down according to the characters in the array)
The Exit prefab has a script attached which ends the maze, updates the current level and returns to the menu. The player prefab has the movement script attached.
What I want to do is:
When the maze loads, timer does nothing (Working)
When the player moves, the timer starts (Working but not best practice - using public static variable - Sets a boolean value to true which will commence countdown - see code below)
When the player reaches the end, stop the timer (Not working! Sets the above bool value back to false however the gui timer keeps going!)
Code below but questions:
How do I stop the GUI timer from updating? I know the value is being set to false ('print()' hints in code) but the timer keeps going?
What is a better way to do it rather than use static var? I know you can use the drag and drop method to access objects but as the player and exit is being Instantiated, I cant do this? I have also tried the gameobject find but doesnt work?
Move script (attached to player - note - notMoved var waits until input is recieved then if it is the first input, sets the playerTime variable to true - starts the timer)
function FixedUpdate () {
var notMoved = true;
var moveHoriz = Input.GetAxis ("Horizontal")*speed;
var moveVert = Input.GetAxis ("Vertical")*speed;
if (notMoved){
if ((moveHoriz != 0) || (moveVert != 0)){
Timer.playerTime = true; //Set player moving to true
}
}
rigidbody.velocity = Vector3 (moveHoriz, 0, moveVert);
}
End script (attached to Maze Exit prefab - Note - sets playerTime to FALSE - This should stop the timer but doesnt?)
function OnCollisionEnter(){
var currentLevel = Application.loadedLevel;
var levelCleared = (PlayerPrefs.GetInt("savedLevel"));
Timer.playerTime = false;
print ("FALSE!");
// PlayerPrefs.SetInt("bestTime"[currentLevel], ?time? ); -- This is to implement (workaround)
yield WaitForSeconds (0.5);
if (levelCleared <= currentLevel){
PlayerPrefs.SetInt("savedLevel",(currentLevel + 1));
PlayerPrefs.Save();
}
Screen.lockCursor = false;
Application.LoadLevel (0);
}
Timer script - attached to 'Startup' game object (Complete script)
private var startTime;
var textTime : String; //added this member variable here so we can access it through other scripts
public static var playerTime = false;
function Awake(){
prepStart();
}
function prepStart(){
while (!playerTime){
yield;
}
startTime = Time.time;
}
function OnGUI () {
if (playerTime){
var guiTime = Time.time - startTime;
var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;
textTime = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction);
}
if ((!playerTime)){
print ("here 1");
}
GUI.Label (Rect (400, 25, 100, 30), textTime); //changed variable name to textTime -->text is not a good variable name since it has other use already
}
Assistance is very much appreciated!
Answer by fafase · Aug 25, 2012 at 09:36 AM
access the var from the other script:
http://answers.unity3d.com/questions/246211/Having-scripts-interact.html
I would instead use:
var timer:Timer;
function Start(){
timer =GameObject.Find("objectName").GetComponent(Timer);
}
function Update(){
if ((moveHoriz != 0) || (moveVert != 0)){
timer.timeElapsedMoving +=Time.deltaTime true;
}
}
Now when your player is moving, the var timeElapsedMoving (float) is added deltaTime. So if you spend 2 s moving....well it is 2.
Your answer
Follow this Question
Related Questions
Timer progress bar 0 Answers
Countdown Timer 1 Answer
GUI elements vanish when publishing 1 Answer
scaling a gui texture 2 Answers
How Would I Make A GUI Label Fade After A Certain Amount Of Time? 1 Answer