Spawn object every 5 coins collected

Hello,

I am trying to write a script to spawn a game object for every 5 coins collected.

I have the spawn working when I collect the first 5 coins. The problem is as long as coinsCollected = 5 then I get multiple spawns because start Spawn code is in Update. As soon as I collect another coin then coinsCollected does not = 5 so the spawning stops. How do I ad a variable or int for C# so the spawing stops immediately.

Also how would I make my script work so that every 5 coins collected a game object will spawn. The game object spawning is always going to be the same game object(like a power up).

Thanks in Advance.

Here is my code:

	public GameObject obj;
	public Vector3 spawnValues;
	private PlayerMove playerMove;
	private GUIManager gui;
	int myInt = 1;
void Start ()
	{
		
		playerMove = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMove>();
		gui = FindObjectOfType(typeof(GUIManager)) as GUIManager ;
		transform.position = playerMove.transform.position;
	}
	
void Update ()
	{
	if (gui.coinsCollected == 5){
		Spawn();
	}
}

void Spawn ()
{
		Instantiate(obj, transform.position, Quaternion.Euler(Vector3.zero));
		//Instantiate
	}
}

You could call Spawn() just after you increase coinsCollected, instead of in the Update function. That way it would only spawn one object every five coins. To make it spawn every five coins you can use a hidden variable that you reset after collecting 5, or do:

if ((gui.coinsCollected % 5) == 0){ // Remainder of a division by 5 equal to 0 means it is true on multiples of 5
}

just change yourcode like this:

int nextSpawn = 5;

...

void Update ()
{
  if (gui.coinsCollected == nextSpawn){
    Spawn();
    nextSpawn+=5;
  }
}