Hi, I have just started learning game dev and this is my first personal project.
void Update()
{
if (!gameManager.gameOver && gameManager.isGameActive)
{
Debug.Log("Spawn Ready: " + spawnReady);
Debug.Log("CS Spawn Ready: " + csSpawnReady);
Debug.Log("Cucumber Time: " + cucumberTime);
if (spawnReady && !csSpawnReady && !cucumberTime)
{
Debug.Log("SPAWN ENEMIES STARTED");
SpawnEnemies();
}
*else if (firstCTime && csSpawnReady && cucumberTime)
{
if ((int)Time.time < cTimeEnd)
{
Debug.Log("FIRST CUCUMBER TIME");
EnemyShower();
}
else
{
cDelay = 20f;
Debug.Log("CUCUMBER TIME OVER!");
Debug.Log("STARTING DELAY");
firstCTime = false;
**DefaultSpawn();**
}
}*
*else if (!firstCTime && csSpawnReady && cucumberTime)
{
Debug.Log("CUCUMBER TIME");
EnemyShower();
**Invoke("DefaultSpawn", cDelay);**
}
SpawnCups();
}*
}
Currently what I want to do is to switch between conditional if statements using the method “DefaultSpawn()” which calls “SpawnEnemies()” or “EnemyShower” and should switch between each other every 60s
DefaultSpawn():
private void DefaultSpawn() { csSpawnReady = false; cucumberTime = false; spawnReady = true; }
SpawnEnemies(): Spawns enemies between a random range of 1s - 2.5s
EnemyShower(): Spawns enemies every 1s
In my first italicized block of code, after the time ends and DefaultSpawn() is called which sets it back to the first if condition, the code runs fine. However, once the second italicized block of code has run its course, and DefaultSpawn() is invoked, while it does correctly go back to the first if condition, my SpawnEnemies() method is runs multiple times each time it is called. The only difference I can think of after troubleshooting is the Invoke() I used to call the DefaultSpawn() method but I don’t understand why or how to fix this.
Some explanation and help would be appreciated.
@logicandchaos
Thanks for your reply and sorry for the late answer I didn’t know I got a reply
I will look into the code refactoring
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
public GameObject enemyPrefab;
public GameObject[] cupPrefab;
public GameObject[] enemyPrefabs;
private bool spawnReady;
private bool cucumberTime;
private bool firstCTime;
private bool csSpawnReady;
private bool isSpawning;
public float cDelay = 10f;
public int cTimeEnd = 20;
// Scripts
private PlayerController playerControllerScript;
private GameManager gameManager;
// Boundaries
private float xBounds;
private float zBoundsMin;
private float zBoundsMax;
private float spawnY = 10f;
// Start is called before the first frame update
void Start()
{
spawnReady = true;
firstCTime = true;
csSpawnReady = false;
cucumberTime = false;
isSpawning = false;
playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
// Spawning Boundaries
xBounds = gameManager.xBounds;
zBoundsMin = gameManager.zBoundMin;
zBoundsMax = gameManager.zBoundMax;
//Invoke("IsSpawnReady", cDelay);
StartCoroutine(SpawnCS());
}
// Update is called once per frame
void Update()
{
if (!gameManager.gameOver && gameManager.isGameActive)
{
Debug.Log("Spawn Ready: " + spawnReady);
Debug.Log("CS Spawn Ready: " + csSpawnReady);
Debug.Log("Cucumber Time: " + cucumberTime);
if (spawnReady && !csSpawnReady && !cucumberTime)
{
Debug.Log("SPAWN ENEMIES STARTED");
SpawnEnemies();
}
else if (firstCTime && csSpawnReady && cucumberTime)
{
if ((int)Time.time < cTimeEnd)
{
Debug.Log("FIRST CUCUMBER TIME");
EnemyShower();
}
else
{
cDelay = 20f;
Debug.Log("CUCUMBER TIME OVER!");
Debug.Log("STARTING DELAY");
firstCTime = false;
DefaultSpawn();
}
}
else if (!firstCTime && csSpawnReady && cucumberTime)
{
Debug.Log("CUCUMBER TIME");
EnemyShower();
Invoke("DefaultSpawn", cDelay);
}
SpawnCups();
}
}
IEnumerator SpawnDefault()
{
yield return new WaitForSeconds(cDelay);
csSpawnReady = false;
cucumberTime = false;
spawnReady = true;
}
IEnumerator SpawnCS()
{
yield return new WaitForSeconds(cDelay);
csSpawnReady = true;
cucumberTime = true;
spawnReady = false;
}
private void DefaultSpawn()
{
csSpawnReady = false;
cucumberTime = false;
spawnReady = true;
}
//private void IsSpawnReady(bool csSpawnReady, bool spawnReady)
//{
// this.csSpawnReady = csSpawnReady;
// //cucumberTime = true;
// this.spawnReady = spawnReady;
//}
private void SpawnCups()
{
float xSpawnRange()
{
float xMin = -xBounds;
float xMax = xBounds;
float xRange = Random.Range(xMin, xMax);
return xRange;
}
float zSpawnRange()
{
float zMin = -zBoundsMin;
float zMax = zBoundsMax;
float zRange = Random.Range(zMin, zMax);
return zRange;
}
int index = Random.Range(0, cupPrefab.Length);
int cupCount = GameObject.FindGameObjectsWithTag("Cup").Length;
Vector3 spawnPos = new Vector3(xSpawnRange(), spawnY, zSpawnRange());
if (cupCount < 5)
{
//Debug.Log("Cup Spawned");
Instantiate(cupPrefab[index], spawnPos, cupPrefab[index].transform.rotation);
}
}
private void SpawnEnemies()
{
// Spawning Boundaries
// Spawn Range
float offset = 10;
// X
float playerXPos = playerControllerScript.transform.position.x;
float xMin = playerXPos - offset;
float xMax = playerXPos + offset;
float spawnX = Random.Range(xMin, xMax);
// Z
float playerZPos = playerControllerScript.transform.position.z;
float zMin = playerZPos - offset;
float zMax = playerZPos + offset;
float spawnZ = Random.Range(zMin, zMax);
float XMax()
{
xMin = xBounds - offset;
xMax = xBounds;
spawnX = Random.Range(xMin, xMax);
return spawnX;
}
float XMin()
{
xMin = -xBounds;
xMax = -xBounds + offset;
spawnX = Random.Range(xMin, xMax);
return spawnX;
}
float ZMax()
{
zMin = zBoundsMax - offset;
zMax = zBoundsMax;
spawnZ = Random.Range(zMin, zMax);
return spawnZ;
}
float ZMin()
{
zMin = zBoundsMin + offset;
zMax = zBoundsMin;
spawnZ = Random.Range(zMin, zMax);
return spawnZ;
}
// Max X Max Z
if (playerXPos >= xBounds - 10 && (playerZPos >= zBoundsMax - offset))
{
//Debug.Log("Max X Max Z is spawning");
Spawn(XMax(), ZMax());
}
// Max X Min Z
else if (playerXPos >= xBounds - 10 && (playerZPos <= zBoundsMin + offset))
{
//Debug.Log("Max X Min Z is spawning");
Spawn(XMax(), ZMin());
}
// Min X Max Z
else if (playerXPos <= -xBounds + 10 && (playerZPos >= zBoundsMax - offset))
{
//Debug.Log("Min X Max Z is spawning");
Spawn(XMin(), ZMax());
}
// Min X Min Z
else if (playerXPos <= -xBounds + 10 && (playerZPos <= zBoundsMin + offset))
{
//Debug.Log("Min X Min Z is spawning");
Spawn(XMin(), ZMin());
}
// Max X
else if (playerXPos >= xBounds - 10)
{
//Debug.Log("Max X is spawning");
Spawn(XMax(), spawnZ);
}
// Min X
else if (playerXPos <= -xBounds + 10)
{
//Debug.Log("Min X is spawning");
Spawn(XMin(), spawnZ);
}
// Max Z
else if (playerZPos >= zBoundsMax - offset)
{
//Debug.Log("Max Z is spawning");
Spawn(spawnX, ZMax());
}
// Min Z
else if (playerZPos <= zBoundsMin + offset)
{
//Debug.Log("Min Z is spawning");
Spawn(spawnX, ZMin());
}
else
{
//Debug.Log("Normal is spawning");
Spawn(spawnX, spawnZ);
}
}
private void Spawn(float spawnX, float spawnZ)
{
float spawnCooldown = Random.Range(1, 2.5f);
Vector3 spawnPos = new Vector3(spawnX, spawnY, spawnZ);
Instantiate(enemyPrefab, spawnPos, enemyPrefab.transform.rotation);
spawnReady = false;
StartCoroutine(SpawnCooldown(spawnCooldown));
//Debug.Log("Spawn Position: " + spawnPos);
}
IEnumerator SpawnCooldown(float spawnCooldown)
{
yield return new WaitForSeconds(spawnCooldown);
//Debug.Log("Spawn Time: " + spawnCooldown);
spawnReady = true;
}
private void EnemyShower()
{
//Debug.Log("Enemy Shower Line 1");
int index = Random.Range(0, enemyPrefabs.Length);
float spawnCooldown = 1.0f;
float xRange = Random.Range(-xBounds, xBounds);
float yRange = 10f;
float zRange = Random.Range(zBoundsMin, zBoundsMax);
Vector3 spawnArea = new Vector3(xRange, yRange, zRange);
spawnReady = false;
cucumberTime = true;
Instantiate(enemyPrefabs[index], spawnArea, enemyPrefabs[index].transform.rotation);
csSpawnReady = false;
StartCoroutine(CSCooldown(spawnCooldown));
//StartCoroutine(FirstCTime());
//Debug.Log("SPAWN!");
}
IEnumerator CSCooldown(float spawnCooldown)
{
yield return new WaitForSeconds(spawnCooldown);
//Debug.Log("Spawn Time: " + spawnCooldown);
csSpawnReady = true;
}
}
Here is the full script