Only run the function once

I have a script (See Below)

#pragma strict

 var SpawnArray : GameObject[];
 var roundedTime : int = 0;
 var difficulty : float = 1.0;
 var gameTime : float = 0.0;
 var difficultyMod : int = 20;
 var lowBlocks : GameObject[];
function Start () 
{
}

function Update () 
{
	if(roundedTime % 5 == 0)
	{
		SpawnNewOBJ();
	}
	gameTime += Time.deltaTime * difficulty;
	roundedTime = Mathf.Round(gameTime);
	if(gameTime > difficultyMod)
	{
		difficulty += 0.5f;
		difficultyMod *= 2;
	}
}
function SpawnNewOBJ()
{		
		var rand : GameObject = SpawnArray[Random.Range(0, SpawnArray.Length)];
		var randBlock : GameObject = lowBlocks[Random.Range(0, lowBlocks.Length)];
		var inst = Instantiate(randBlock, new Vector2(rand.gameObject.transform.position.x, rand.gameObject.transform.position.y), Quaternion.identity);
}

What I am trying to do is when the rounded time is a divisible of 5, it spawns 1 object. But what is happening is it runs the function"SpawnNewOBJ" a million times because the rounded time stays divisible by 5 for 1 second. May be hard to understand sorry. How would I make it so the function only gets called once?

The simplest thing I can think about is using a lock.

declare it as an exposed variable:

var lock : boolean = false;

Set as true when you’re spawning:

function SpawnNewOBJ(){        
    lock = true;
    //The rest of your code as is...
}

Remove it when new reassignment takes place:

roundedTime = Mathf.Round(gameTime);
lock = false;

Add it as a condition for the function’s call:

if(roundedTime % 5 == 0 && !lock)

Hope this helps.

For me the easiest way to do that is simply to make something like that :

     if(roundedTime % 6 == 0)
     {
         SpawnNewOBJ();
         gameTime += 1;
     }

Or to retake the previous answer :

var lock : bool = false;
var prevTime : int = 0;

     function Update () 
     {
         if(roundedTime % 5 == 0 && !lock)
         {
             lock = true;
             SpawnNewOBJ();
         }
         prevTime = roundedTime ;
         gameTime += Time.deltaTime * difficulty;
         roundedTime = Mathf.Round(gameTime);
         if (prevTime  != roundedTime )
             lock  = false;
         if(gameTime > difficultyMod)
         {
             difficulty += 0.5f;
             difficultyMod *= 2;
         }
     }
     function SpawnNewOBJ()
     {        
             var rand : GameObject = SpawnArray[Random.Range(0, SpawnArray.Length)];
             var randBlock : GameObject = lowBlocks[Random.Range(0, lowBlocks.Length)];
             var inst = Instantiate(randBlock, new Vector2(rand.gameObject.transform.position.x, rand.gameObject.transform.position.y), Quaternion.identity);
     }

Hope this help…