Part of your issue probably lies in the fact that Time.timeSinceLevelLoad is a float, meaning it can have decimal places. Since you're calling this in FixedUpdate, which is called in every fixed update frame, you're not always going to get a rounded number. More than likely, Time.timeSinceLevelLoad % 3 is working out to something like 0.005, meaning it isn't exactly 0. As well, comparison between floats using the equal operator isn't very accurate - you should always use
Mathf.Approximately for that.
One thing you can give a try for this is a coroutine. They're very useful in Unity, and they fit what you're trying to do quite well. Just set up a function that looks like this:
function SpawnEnemies() {
while (true) {
yield WaitForSeconds(3);
var position = Vector3(Random.Range(-5, 5), .5, 80);
Instantiate(prefab, position, Quaternion.identity);
}
}
This code waits for 3 seconds, then spawns an enemy using your code, and just repeats that indefinitely. "yield" lets other scripts run while this function stays as a coroutine, meaning that the infinite loop won't freeze your game - it'll just keep spawning an enemy every 3 seconds. To start the function, just call it once in a script's Start function like so:
function Start() {
SpawnEnemies();
//Or, if you're using C#, call this:
//StartCoroutine(SpawnEnemies());
}
And that's it! Hope that helps. Let me know if any of this confuses you and I'll try to help.
