Should I revise my scripts or leave it alone?

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?

I just realized I did exactly what Rick did when i made the StopSpawning method albeit slightly differently but I even wrote it the same as him lol

Hi Xarce,

Good job on challenging yourself. :slight_smile:

In many cases, there are multiple ways to make something work in Unity, and Rick cannot show all of them. If your code works, it’s a solution by definition. Also, as long as you are understanding the underlying concepts of this game, you will very likely be able to solve future problems that are caused by your current solution. As a game developer, your job is to solve problems, so problems are perfectly fine. They are nothing to worry about, in my opinion.

FindObjectOfType is one of the slowest methods in Unity. Is it really necessary to call that method each time OnTriggerEnter2D gets called? (There is no universal answer to this question because it depends on the context!)

Hopefully, this helped. :slight_smile:


See also:

1 Like

I will keep that in mind! Your responses have been extremely helpful thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms