Using Time.time in an if statement?

I’m trying to use the following code to wait for two and a half seconds before playing an audio clip attached to the same GameObject:

function Update() {
	var PlayTime = 2.5;
	if (Time.time = PlayTime) {
		audio.Play();
	}
}

I get the following error:
(3, 23) BCE0044 expecting ) found ‘=’
Fixing this error (changing it to (Time.time) = PlayTime )causes another error:
(3, 24) BCE0043 unexpected token =

Any idea what is wrong? I know I saw Time.time used in a similar way in a scripting reference example.

if (Time.time = PlayTime) {

should be if (Time.time == PlayTime) {

That won’t work anyway, since Time.time will almost certainly never equal exactly 2.5. Don’t use Update at all, use yield WaitForSeconds in Start.

–Eric

I’d suggest the coroutine route as well, however if you don’t feel confident enough to travel down that path yet, you should alter your if test to:

if (Time.time >= PlayTime) 
{ 
   audio.Play(); 
}

You need to be careful though as this will continue to call the play function after this, so using a coroutine with WaitForSeconds will probably be the safer route to go.