- Home /
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
using UnityEngine;
using System.Collections;
public class cointopon : $$anonymous$$onoBehaviour
{
public Transform[] spawnPoints;
public GameObject[] enemyPrefabs;
public float amountEnemies = 20; // Total number of enemies to spawn.
public float yieldTime$$anonymous$$in = 0;
public float yieldTime$$anonymous$$ax = 5; // Don't exceed this amount of time between spawning enemies randomly.
public int i;
public bool state;
void Update (){
yieldTime$$anonymous$$in = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinbool$$anonymous$$").Value;
yieldTime$$anonymous$$ax = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinboolmax").Value;
amountEnemies = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinboolamount").Value;
state = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmBool("coinbool").Value;
if (state == true){
StartCoroutine("Spawnblocks");
}
else if (state == false) {
StopCoroutine("Spawnblocks");
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(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax)); // 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);
yield return null;
}}}
Change your yieldTime$$anonymous$$in and yieldTime$$anonymous$$ax to numbers which you find more suitable.
Answer by powerproc_chris · Apr 16, 2013 at 03:55 PM
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!
Thanks for everyone help but one more problem IndexOutOfRange exception here is my new code
using UnityEngine;
using System.Collections;
public class cointopon : $$anonymous$$onoBehaviour
{
public Transform[] spawnPoints;
public GameObject[] enemyPrefabs;
public float amountEnemies = 20; // Total number of enemies to spawn.
public float yieldTime$$anonymous$$in = 0;
public float yieldTime$$anonymous$$ax = 5; // Don't exceed this amount of time between spawning enemies randomly.
public int i;
public bool state;
private bool alreadySpawning = false;
bool waitSpawn=false;
void Update (){
yieldTime$$anonymous$$in = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinbool$$anonymous$$").Value;
yieldTime$$anonymous$$ax = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinboolmax").Value;
amountEnemies = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinboolamount").Value;
state = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmBool("coinbool").Value;
//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;
}
}
public IEnumerator Spawnblocks() {
for (i=0; i<amountEnemies; i++){
var randNum = Random.Range(0, spawnPoints.Length);
yield return new WaitForSeconds(Random.Range(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax)); // 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);
waitSpawn=false;
yield return null;
}}}
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]
.