Hi there,
I'm still experiencing issues with setting up a time bar with the UI Demo scene. I have an existing timer that works and I've copied the progress bar into my scene as well as the a portion of UIDemoController script into my timer script. The progress bar doesn't seem to be work, is there an issue with my script or do I need to attach it to a different Game Object?
Here is my timer script:
public static float curSeconds = 59;
public static float curMinutes = 4;
public static float startSeconds = 0;
public static float startMinutes = 0;
public static float startTime;
public static int finishTime;
public static float currentTime;
public static float roundedRestSeconds;
public static float displaySeconds;
public static float displayMinutes;
public static float countDownSeconds;
public static float restSeconds;
public tk2dUIProgressBar progressBar; //UI Demo code
private const float TIME_TO_COMPLETE_PROGRESS_BAR = 2f; //UI Demo code
private float progressBarChaseVelocity = 0.0f; //UI Demo code
void Start () {
GameObject.Find ("TimerText").guiText.text = "Time Remaining: " + curMinutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
guiText.material.color = Color.black;
startTime = Time.time;
finishTime = (int)(Time.time);
}
void OnLevelWasLoaded(int level) {
if (level ==
{
GameObject.Find ("TimeSpentText").guiText.text = " " + finishTime.ToString();
guiText.material.color = Color.black;
}
}
void Update () {
currentTime = Time.time - startTime;
restSeconds = countDownSeconds - (currentTime);
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = roundedRestSeconds / 60;
string text = string.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
GameObject.Find ("TimerText").guiText.text = " " + text.ToString();
guiText.material.color = Color.black;
startTime = startMinutes + startSeconds;
startTime = Time.time;
if(curSeconds <= 0) {
curSeconds = 59;
if(curMinutes >= 1)
{
curMinutes--;
}
else {
curMinutes = 0;
curSeconds = 0;
Application.LoadLevel("S1P1"); // If time runs out, the player will need to restart the level
Score.score = 0; //It will also reset the score
curMinutes = 4; //And reset the time
curSeconds = 59;
// This makes the guiText show the time as X:XX. ToString.("f0) formats it so there is no decimal place.
GameObject.Find ("TimerText").guiText.text = curMinutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
}
}
else {
curSeconds -= Time.deltaTime;
}
//These lines will make sure the time is shown as X:XX and not X:XX.XXXXXX
if(Mathf.Round(curSeconds) <= 9) {
GameObject.Find ("TimerText").guiText.text = curMinutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
}
else {
GameObject.Find ("TimerText").guiText.text = curMinutes.ToString("f0") + ":" + curSeconds.ToString("f0");
}
}
private IEnumerator MoveProgressBar() //Altered UI Demo code
{
while (progressBar.Value < 2f)
{
progressBar.Value = startTime/TIME_TO_COMPLETE_PROGRESS_BAR;
yield return null;
startTime += tk2dUITime.deltaTime;
}
while (progressBar.Value < 0.0f)
{
float smoothTime = 0.5f;
progressBar.Value = Mathf.SmoothDamp( progressBar.Value, progressBarChaseVelocity, ref smoothTime, Mathf.Infinity, tk2dUITime.deltaTime );
yield return 0;
}
}
}
I'm still learning how to script, so please let me know where I am going wrong?
Thanks.