Stop instantiating after 5 coins for few seconds

Hi All,

I have some trouble adding some code to my javascript. I am a script noob as you can see :wink:

The TouchControl.js script is working perfect when I touch there is a coin instantieating “spawn” at touch position.
Now I want to add the following, when you have touched 5 times it will wait 5 seconds than you can instantiate again for 5 times.
I added a variable “coincounter” and I added “coin counter += 1” in the script so every time I touch it will add 1.
Now I have to check if coincount is equal to 5 then do something like WaitForSeconds and reset the variable “coin counter” to zero again.
I think I have to add another if statement but have no clue where.

I hope i’m right so far and have to put this piece of code to my TouchControl.js somewhere.

Hope someone can help me.

if (coincounter == 5)
	{
	yield WaitForSeconds (5.0);
	coincounter = 0;
	}

TouchControl.js

public var coinprefab : Transform;
var coincounter = 0;

var layerMask = 1 << 8;

function Update () {
	for (var Touch : Touch in Input.touches) {
		if (Touch.phase == TouchPhase.Began) {
			
			var ray = Camera.main.ScreenPointToRay (Touch.position);
			var hit: RaycastHit;
			
			if (Physics.Raycast (ray, hit, layerMask)) {
					PoolManager.Pools["Money"].Spawn(coinprefab, hit.point, transform.rotation);
					scoreScript.score -= 1;
					coincounter += 1;
					
			}
		}
	}
}

You are probably better having a cool down of some sort… some rough code as a guide:

update() {

if (cooldown > 0)
  cooldown -= Time.deltaTime;

...

if (Physics.Raycast (ray, hit, layerMask)  cooldown <= 0) {

...

coincounter += 1
if (coincounter == 5) {
  cooldown = 5; coincounter = 0;
}

Hi JohnnyA,

Thanks for your reply!

I did the following it gives me no error and is working after I set the cooldown to 150.
It’s goes from 150 to 0 very quickly not in seconds so 5 was a very fast cooldown.

I don’t mind to set it to 150 it is working but did I do something wrong with Time.deltaTime?

public var coinprefab : Transform;
var coincounter = 0;
var cooldown = 0;

var layerMask = 1 << 8;

function Update () {

	if (cooldown > 0)
		cooldown -= Time.deltaTime;
	
	for (var Touch : Touch in Input.touches) {
		if (Touch.phase == TouchPhase.Began) {
			
			var ray = Camera.main.ScreenPointToRay (Touch.position);
			var hit: RaycastHit;
			
			if (Physics.Raycast (ray, hit, layerMask)  cooldown <= 0) {
					PoolManager.Pools["Money"].Spawn(coinprefab, hit.point, transform.rotation);
					scoreScript.score -= 1;
					coincounter += 1;
					
				if (coincounter == 5){
					cooldown = 150;
					coincounter = 0;
				}
			}
		}
	}
}