Unity – Timers and Previous Time C#

Ok, so I was play testing the crap out of AstroBall and my poor wife was also dragged into play-test hell over the weekend (and the last four weeks lol).  A nice feeling when you see someone genuinely enjoying something you have created, struggling on what to me was pretty easy as I had navigated every corner and danger section to the tenth degree.

My wife stopped and said to me, “What about a timer?, adds a little pressure to do better each time?”.

If I am honest, I had thought of a timer early into development.  I had googled the crap out of it. Bear in mind I have limited coding skills and have managed to swim through the basics relatively easily. However, timers seemed to elude me.

If you google “Unity Timers” or “Unity save best time”, you get lots of threads on the subject but nothing with examples which in my case explained very simply how to code and display a timer with a best time saved below it on screen.

This is what I came up with:

using UnityEngine;
using UnityEngine.UI;

public class Timerlvl1 : MonoBehaviour
{
public Text timertext; //Text for GUI
public Text pttext; // text for GUI
public float levelTimer;
public bool updateTimer = true;

public void Start()
{
//updateTimer = true;
levelTimer = 0;
pttext.text = “Prev Time: ” + PlayerPrefs.GetFloat(“PreviousTime1”).ToString(“f1”);
}

public void Update()
{
if (updateTimer)
levelTimer += Time.deltaTime;
timertext.text = “Time: ” + levelTimer.ToString(“f1”); //Time to texture with 2 decimal
}

public void Finish()
{

updateTimer = false;
timertext.color = Color.green;
PlayerPrefs.SetFloat(“PreviousTime1”, levelTimer); //Keep Complete Time for compare and save

Debug.Log(updateTimer);
}

}

It works.  It works because it is very mundane and until I get my head around “Manager” scripts and instances, this will suffice.  It works by grabbing starting the timer and passing the same data to ‘updateTimer’.  this then stores it as a PlayerPref (this would need more explanation later but for now it is what I am using).  then in turn I use a simple key to establish the data back to the level so when you reload the level the previous (or best time) is displayed.

Create two text objects and tailor the colour and style etc. Add those to the inspector attributes and also make sure you create a script called “Finish” and add this to the finish object with collider enabled.

Example:

 

using UnityEngine;

public class Finish : MonoBehaviour {

private void OnTriggerEnter(Collider other)
{
GameObject.Find(“Player”).SendMessage(“Finish”);
}
}

As soon as the “Player crosses into the collider boundary the timer stops and then the previous (or best) time is saved as a PlayerPref (in this case as – PreviousTime).  as of now, I am creating a individual ‘Timer’ scripts for each level, i.e. Timerlvl1.  Timerlvl2 and so on and so forth…

… but again, it works for me and I know it will probably help someone else.

Anyone with an ounce of coding skill will be able to pick this apart and probably find a redundant piece of code which could probably be removed or at least cleaned up.

I really hope the above helps someone who is Struggling to incorporate a Unity Timer or at least give them the tools /code to get around creating a timer with (best?) previous time display.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s