If someone feels like helping me out, would be greatly appreciated
I made for the Glitch Garden part of the course a custom spawner script.
In the script i have a method to sets a bool to false, just like the spawners Rick made.
In Start, it does work and sets the bool to true, the method is being called,
but for some reason it does not change the bool…
Its only called in Start, in Update (if false return) and in the while loop, like ricks spawners
I used debug.log to see if the method is being called, and it is…
I do not get it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CustomSpawner : MonoBehaviour
{
[SerializeField] float totalLevelTimeAfterFirstSpawn = 20f;
[SerializeField] float timeBeforeFirstSpawn = 5f;
[SerializeField] float minSpawnInterval = 8f;
[SerializeField] float maxSpawnInterval = 8.1f;
[SerializeField] float spawnIntervalIncreaseAmount = 1f;
[SerializeField] float spawnIntervalIncreaseTime = 40f;
float spawnIntervalIncreaseTimeR;
[SerializeField] Attacker[] attackerArray;
[SerializeField] bool spawn;
[SerializeField] GameObject spawner;
[SerializeField] GameObject spawner1;
[SerializeField] GameObject spawner2;
[SerializeField] GameObject spawner3;
[SerializeField] GameObject spawner4;
[SerializeField] GameObject spawner5;
Queue<int> _pool = new Queue<int>();
int _number = 0;
int attackerIndex = -1;
GameTimer gameTimer;
public float TimeBeforeFirstSpawn()
{
return timeBeforeFirstSpawn;
}
void Start()
{
spawn = true;
spawnIntervalIncreaseTimeR = spawnIntervalIncreaseTime;
gameTimer = FindObjectOfType<GameTimer>();
gameTimer.SetGameTime(totalLevelTimeAfterFirstSpawn);
if (SceneManager.GetActiveScene().name != "Scene_03_Glitch")
{
StartGame();
}
}
private void Update()
{
if(spawn == false) { return; }
spawnIntervalIncreaseTimeR -= Time.deltaTime;
if(spawnIntervalIncreaseTimeR <= 0)
{
minSpawnInterval -= spawnIntervalIncreaseAmount;
maxSpawnInterval -= spawnIntervalIncreaseAmount;
spawnIntervalIncreaseTimeR = spawnIntervalIncreaseTime;
}
}
public void StartGame()
{
Invoke("InvokeCoroutineAfterDelay", timeBeforeFirstSpawn);
}
void InvokeCoroutineAfterDelay()
{
SpawnAttacker();
gameTimer.GameStarted();
StartCoroutine(SpawnCoroutine());
}
IEnumerator SpawnCoroutine()
{
while (spawn)
{
yield return new WaitForSeconds(Random.Range(minSpawnInterval, maxSpawnInterval));
SpawnAttacker();
}
}
void SpawnAttacker()
{
attackerIndex++;
if(attackerIndex < attackerArray.Length)
{
PickSpawnerToInstantiateEnemy(attackerArray[attackerIndex]);
}
}
//pick spawner at random and child supplied attacker at that spawner
public void PickSpawnerToInstantiateEnemy(Attacker attacker)
{
Attacker newAttacker;
if (_pool.Count == 0)// if pool is empty
{
// generate new pool:
_pool = GiveMeNonReapeatingRandomSet(0, 6);
// make sure first number in pool is not the same as our last one:
if (_pool.Peek() == _number)
{
_pool.Enqueue(_pool.Dequeue());
}
}
// pull next number from the pool:
_number = _pool.Dequeue();
switch (_number)
{
case 5:
newAttacker = Instantiate(attacker, spawner.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
newAttacker.transform.parent = spawner.transform;
break;
case 4:
newAttacker = Instantiate(attacker, spawner1.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
newAttacker.transform.parent = spawner1.transform;
break;
case 3:
newAttacker = Instantiate(attacker, spawner2.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
newAttacker.transform.parent = spawner2.transform;
break;
case 2:
newAttacker = Instantiate(attacker, spawner3.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
newAttacker.transform.parent = spawner3.transform;
break;
case 1:
newAttacker = Instantiate(attacker, spawner4.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
newAttacker.transform.parent = spawner4.transform;
break;
case 0:
newAttacker = Instantiate(attacker, spawner5.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
newAttacker.transform.parent = spawner5.transform;
break;
default:
Debug.Log("PickRandomSpawner Default reached");
break;
}
}
Queue<int> GiveMeNonReapeatingRandomSet(int min, int max)
{
HashSet<int> set = new HashSet<int>();
while (set.Count != Mathf.Abs(max - min))
{
set.Add(Random.Range(min, max));
}
return new Queue<int>(set);
}
public void StopSpawning()
{
Debug.Log("stop spawning called");
spawn = false;
}
}