Stop spawning on bool false and enable on true?

Hello, how can i turn off this by using a bool to stop spawning and then make it spawn when the bool is true? i am getting no error’s but it is not working? here is my code. This is C#

using UnityEngine;
using System.Collections;
 
public class cointopon : MonoBehaviour
{
 
  public Transform[] spawnPoints;
  public GameObject[] enemyPrefabs;
  public float amountEnemies = 20;  // Total number of enemies to spawn.
  public float yieldTimeMin = 0;
  public float  yieldTimeMax = 5;  // Don't exceed this amount of time between spawning enemies randomly.
  public int i;
  public bool state;
  
	void Update (){
	yieldTimeMin =  PlayMakerGlobals.Instance.Variables.GetFsmFloat("coinboolmin").Value;
	yieldTimeMax = 	PlayMakerGlobals.Instance.Variables.GetFsmFloat("coinboolmax").Value;
	amountEnemies = PlayMakerGlobals.Instance.Variables.GetFsmFloat("coinboolamount").Value;
	state = PlayMakerGlobals.Instance.Variables.GetFsmBool("coinbool").Value;
	if (state == true){
	Spawnblocks();
	}	
	else if (state == false) {
    Debug.Log ("false");	
	}
	}
	
    
	  public IEnumerator Spawnblocks() {
		
      for (i=0; i<amountEnemies; i++){
			
	    var randNum = Random.Range(0, spawnPoints.Length);
        yield return new WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.
 
        var obj = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)]; // Randomize the different enemies to instantiate.
        var pos = spawnPoints[randNum];
 
        Instantiate(obj, pos.position, pos.rotation); 
			
}}}

Spawnblocks() is enumerator, so use start coroutine, and also add Yield return null; at the end of coroutine. When false use stop coroutine

–

Can you please show code?

–

thanks it works, but i spawns to many to fast?

–

Should i use a late Update?

–

What can i do to make it not spawn so much?

–

1 Answer

1

From geronika2004’s updates to your code, I believe you would still need a flag to check if spawning is already occurring:

private bool alreadySpawning = false;

void Update() {
    ...
    //Check if should be spawning and is not already spawning
    if (state && !alreadySpawning) {
        //Start the spawn and set the flag
        StartCoroutine(Spawnblocks());
        alreadySpawning = true;
    } else if (!state && alreadySpawning) { //Check if should not be spawning and is already spawning
        //Stop the spawn and set the flag
        StopCoroutine("Spawnblocks");
        alreadySpawning = false;
    }
}

I think this would work off the top of my head (hopefully). Any questions/comments and I will gladly respond! =)

Assets/cointopon.cs(24,9): error CS0103: The name `start' does not exist in the current context

–

I edited my answer, I accidentally replaced "state" with "start" lol.. Oops!

–

What line is the error occurring on? I'm going to take a guess and say either 41, var obj = enemyPrefabs[...], or 42, var pos = spawnPoints[randNum].

–

41 is the problem.

–

line 41 that is

–