As part of the challenge, I was attempting to figure out what code needed to be added on my own. My code ended up being a bit different from the code in the lecture and I’m wondering what the pros and cons are of doing it the way I did vs how Rick did it.
My Level.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Level : MonoBehaviour {
[SerializeField] int BreakingBlocksRemaining; // Serialized for debugging purposes only
SceneLoader SceneLoader;
// Start is called before the first frame update
void Start() {
SceneLoader = FindObjectOfType<SceneLoader>();
}
public void CountBreakableBlocks() {
BreakingBlocksRemaining++;
}
public void BlockDestroyed() {
BreakingBlocksRemaining--;
if (BreakingBlocksRemaining <= 0) {
SceneLoader.LoadNextScene();
}
}
}
As you can see in my Start method I used FindObjectOfType instead of GetComponent. Originally I had GetComponent but was getting errors, so I switched it to get it to work. I did not add the SceneLoader script as a component to my Level GameObject, but I did realize that I had Prefab’d my SceneLoader script and had that in each of my two levels as a GameObject. So instead of getting the component from the current object, it found the object in the entire scene.
I understand that in programming there are usually at least a dozen ways to do anything, I’m just curious what the pros/cons are to these two methods given that they (seem to) give identical output.
Thanks.