Instantiating an object based on time

Hello Unity community. I’m new here and quite new to Unity, but I’m set on making a game and eventually getting it up iTunes. Anyways, I’ve run into a problem that seems like the answer would be quite simple but I was hoping not only to get a solution but to also get an answer as to why this doesn’t work as I thought it would. I’m trying to instantiate an object based on whatever time it is since the level has loaded.

public var newObject:GameObject;
function Update () {
if(Time.time == 5.0) {
	var objectInstance = Instantiate(newObject, transform.position, transform.rotation);
	Debug.Log("Object Created");
}
}

Thanks!

I suspect this boils down to the update loop - Time.time may not necessarily ever == 5.0 as this Update() (which runs every frame) is not stable time wise, so you may be getting 4.99991 or 5.00001.

You may find better results with FixedUpdate() but my suggestion would be to just do > and use a bool to stop it happening multiple times.

Your time check needs to use >=

if ( Time.time >= 5.0 )

Frames don’t have exact timing of fractions of seconds, so Time.time being an exact number of seconds would be pretty unlikely

Also, Time.time is the time since the game started, the whole game. If your need time since loading a level, there is another variable called Time.timeSinceLevelLoad

So, your code should be

if ( Time.timeSinceLevelLoad >= 5.0f )