SceneLoader GetComponent<SceneLoader> vs FindObjectOfType<SceneLoader>

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.

Hi Tom,

What two methods do you mean? Unless I misunderstood you, FindObjectOfType returned an object whereas GetComponent returned null. The output is not identical.

FindObjectOfType returns a gameobject in your scene of the type specified. GetComponent returns a component attached to the gameobject the script is running on. So it depends on how you set things up if you make the loader a separate gameobject you will need to use FindObjectOfType, if you attach the loader to the same gameobject then you can use GetComponent.

Method one: Have a Scene Loader GameObject in my Hierarchy, do not have the SceneLoader script as a component to my Level GameObject, and use FindObjectOfType within the Level script.

Method two: Do not have a Scene Loader GameObject in my Hierarchy, have the SceneLoader script as a component to my Level GameObject, and use GetComponent within the Level script.

Okay, that all makes sense. Do you know if there are pros/cons to doing it one way over another? Sometimes it’s hard to tell in a project this small if one method will end having performance issues, or something like that.

GetComponent is more performant than FindObjectOfType because the latter looks for an object in the entire scene. The more objects there are in your scene, the longer it needs.

Depending on what the SceneLoader method contains, you could create a normal C# class (meaning a class which does not inherit from a Unity class) and make it static or a singleton. That would be even more performant than GetComponent and FindObjectOfType.

That makes a lot of sense. Thanks for the help!

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