Best Practice for World Time and Game Timers C#

What are best practices for displaying Time.time on a GUI Element? I’m using it to keep track of the game time, and I’m pretty sure Update() isn’t the right choice.

using UnityEngine;
using System.Collections;

public class TestScript : MonoBehaviour 
{

	public float health;
	
	public GUIText consoleGUI;
	public GUIText realTimeGUI;
	public GUIText positionGUI;
	public GUIText healthGUI;
	
	
	public float worldTime;
	public float realTimeCount;
	
	public GameObject playerLocation;
	
	
	
	void Start () 
	{
		health = 100f;
	
	}
	
	void Update () 
	{
	
		worldTime = Time.time *2;
		realTimeCount = Time.time;

                if (worldTime == 3.0f)
		{
			Debug.Log("Print Something.");
		}
		
		consoleGUI.text  =  "Real Time: " + realTimeCount.ToString();
		realTimeGUI.text =  "Game Time: " + worldTime.ToString();
		
		
		positionGUI.text = "World Position: " + playerLocation.transform.position;

I also tried an if statement that would debug.log a message if worldTime hits 3.0f, but I don’t get anything on the console. I tried with 0, and that works, but anything after that doesn’t…any ideas on what I’m understanding wrong? Thanks in advance.

Because the chances of a frame executing at exactly 3 seconds are rather low - which is only compounded by the unreliability of floating point comparisons (meaning - 3.00001 is not equal to 3.0000000001).

If you want to delay execution of something then you should either use Invoke or make a coroutine that waits a designated amount of time.

Thanks for the response. What I really want is something to happen once the counter reaches a particular number. The counter’s speed changes based on a rate set in a separate variable.

Now I understand that floating point comparisons are never accurate, but how can I trigger something to happen at around 3 seconds (after increasing the speed)? Can I establish how quick seconds pass with coroutines? Doesn’t have to be exactly 3 seconds, but somewhere close would work.

In my test game, I’m treating “thirst” like an incrementing counter, once it reaches 100…character dies. The counter speeds up sometimes depending on the character’s physical actions. Would coroutines still be my best option?

public float thirst;

public class refreshRatesData
	{
		public float thirstRefreshRate;
              // More Refresh Rates


	}
	
	public refreshRatesData refreshRates;

// Bool Checkers
	
	public bool isDrinking;


	void Start () 
	{
		thirst = 0;
		refreshRates.thirstRefreshRate = 0.04f;
		isDrinking = false;
	}

private void thirstIncrease()
	{
		if(isDrinking == false)
		{
			thirst += Time.deltaTime *refreshRates.thirstRefreshRate;
		}
	}

If you want it to happen once the seconds reach three, just do something like:

bool eventHappened = false;  // Member variable
...
// In Update
if(worldTime >= 3.0f  !eventHappened)
{
  eventHappened = true;
  Debug.Log("Print Something");
}

For several different events that happened at different points on the timeline, you could use a single lastEventTime instead of multiple flags.

Excellent, that works, and puts me on the right track. Thanks!