FindObjectsOfType(typeof())

So i came across this in my programming book i am reading can someone make it any more clear on what the author is saying?

GetComponent(Player);
If the function expected the type player and not the object player, you’d get an error. There’s a subtle difference here. The type of an object is not the object. However, it’s difficult to say “this is the type Child” and not the “Child” class object. The two things use the same word, so it’s easy to confuse.
There is a function that will get the type of an object and satisfy functions that need types and not objects. And that’s where typeof(Child) comes in. If we have the object Child typeof(Child), we can put Child into the typeof() function and use that function as the type of Child and not the class Child.
Give the FindObjectsOfType() function a typeof(Child) and it returns an array with every instance of Child in the scene. We can give the Example.cs a GameObject ChildObject; to chase after. Just so we don’t get confused what version of the word Player we’re looking for we should make it tremendously clear what we’re looking for.

Yeah, it’s pretty confusing the way the author tried explaining it.
Basically, there’s an object defined in the System namespace called Type which contains information that describes an object - such as what properties, fields, methods are defined for that object. You can always get this Type object by using the typeof(x) function where x is the name of an object in scope.

For example, if I have and object defined as:

public class Player : MonoBehaviour
{
    public string Name;
}

I can get a Type object that describes this class like so:

Type playerType = typeof(Player);

The author is pointing out that Unity’s GetComponent() and FindObjectsOfType() functions take in a Type object as a parameter - so this is wrong (won’t even compile):

Player myPlayerInstance = new Player();
var myComponent = GetComponent(myPlayerInstance);

And this is right:

var myComponent = GetComponent(typeof(Player));

Although (not to confuse you too much) this would also work:

Player myPlayerInstance = new Player();
var myComponent = GetComponent(myPlayerInstance.GetType());

Since every object has a GetType() function which will return its Type object - just make sure you call that from an actual instance of an object; if it’s null you’ll get a null reference exception during runtime.

That help any?

1 Like

Great Explanation.

Thanks that helped alot i can imagine this as a parent - child relationship where the instances are the children and the types are the parent where the children originally come from, but what exactly are you doing here

  • Type playerType = typeof(Player);

Didn’t we already go over this in this thread? I believe the end result was instructions to burn the book and find a better learning resource.

Does anyone know why these functions or methods dont accept instances of an object(Type)

They don’t accept instances of an object because they aren’t looking for specific objects, they’re looking for anything of a certain type.

I see.

Well, the statement is declaring a variable of type Type called playerType and assigning it the result of typeof(Player), same as you would declare and assign any other variable like:

int answerToLifeTheUniverseAndEverything = 42;

Technically, Type is actually an object, and that is the type of object that these methods accept. For example, the definition for the FindObjectsOfType() method looks something like this:

public static Object FindObjectOfType(Type type)
{
    // Some code guts and whatnot.
}

Where you can see that it accepts an object instance of type Type called… well, type.

Similarly, I could define my own method to, say, output the assembly qualified name of a type in Unity’s console window like so:

using UnityEngine;

public static class TypeHelper
{
    public static void OutputQualifiedName(Type objectType)
    {
        Debug.Log(objectType.AssemblyQualifiedName);
    }
}

Which, if I call by passing along the Type object which describes the Player type:

TypeHelper.OutputQualifiedName(typeof(Player));

I’ll see something like:
Player, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Logged in the console window.

Let’s go all the way through to reflection, where you can handle any member as an object. That muddies the water further.

I would suggest forgetting about understanding this in detail until you have a few scripts under your belt. It’s really not beginner material.