using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelController : MonoBehaviour
{
GameTimer gameTimer;
int listOfEnemies;
bool isItDone = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
listOfEnemies = FindObjectsOfType<Attacker>().Length;
gameTimer = FindObjectOfType<GameTimer>();
isItDone = gameTimer.dingDong;
TimerListen();
}
private void TimerListen()
{
if (isItDone)
{
var spawners = FindObjectsOfType<AttackerSpawner>();
foreach (AttackerSpawner spawner in spawners)
{
spawner.StopSpawning();
}
CheckForEnemies();
}
else { return; }
}
private void CheckForEnemies()
{
if(listOfEnemies <= 0)
{
Debug.Log("END DA WORLY");
}
else { return; }
}
}
I made a method for the AttackerSpawner that changed the spawning bool and nested the statements inside the Spawn method into an if statement that was conditional on the spawn bool since for whatever reason the start coroutine just did not want to listen to the spawn bool being turned false.
I can see that Rick did it completely differently but in the end after making some slight changes and taking inspiration from other scripts I got the desired results plus I made it so that enemies are destroyed upon collision with the base
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseScript : MonoBehaviour
{
[SerializeField] int negationAmount = 1;
private void OnTriggerEnter2D(Collider2D collision)
{
var enemy = collision.gameObject;
Object.Destroy(enemy);
var livesScript = FindObjectOfType<Lives>();
livesScript.NegateLives(negationAmount);
}
}
I might be ommitting some things so please ask if needed. But should I scrap all my methods and write it according to Rick’s version or will I be able to proceed with the lesson no problem?
I’m glad I did the challenge by myself but I am weary to proceed if it affects later lessons in this section…
Also is my version less efficient for making the LevelController do most of the work on the fly instead of having numerous scripts call upon it like rick did? I mean it’s like the same thing but the LevelController does the calling so I guess Rick cut out the middle man by having the objects call to it and provide information instead of having my version which is constantly checking yes?