[Solved] What does GameObject.FindObjectOfType<>() actually return?

Hello,

In this lecture we’ve made sure that the ball can be launched when the mouse is clicked. Everything works fine for me, but there’s just some details regarding the code that I don’t quite understand, even after looking around a bit on the web. The problem: I don’t quite get what we assign to the paddle variable in the Start function. The code is this:

paddle = GameObject.FindObjectOfType<Paddle>();

A couple of questions regarding this code:

  1. What does GameObject refer to here? The current GameObject (which would be the ball, since we’re in ball.cs and this is attached to the Ball GameObject)? Or does it refer to the GameObject class, so we can call the function FindObjectOfType<>()?
  2. What sort of Object are we finding when we use FindObjectOfType? A GameObject? A script? I’ve fiddled around with renaming certain things to see when it would and wouldn’t work, but I haven’t quite been able to figure it out.
  3. What is it that the entirety of GameObject.FindObjectOfType<Paddle>() then refers to? I suppose it must be a GameObject, since we’re able to access its position by using paddle.transform.position in the following code. However, we’re assigning this to a variable of type Paddle, which refers to a script, so how can that be a GameObject?

I hope I’ve been able to elucidate just what I’m struggling with here, and I hope someone will be able to help me.

Thanks!

I THINK it returns the class of the type paddle from the gameobject paddel in the scene.

Since this is for me as new as for you, this statement needs conformation (or correction) from someone more knowledgeable . :slight_smile:

I’m afraid I can’t provide a full answer either, but hopefully some elements of one.

First, as you say, GameObject refers to the class, and it is here simply so that we can access the static FindObjectOfType() function. However, it is redundant in this case, since FindObjectOfType() is a static function of the Object class, from which GameObject inherits, but so does Monobehaviour. Therefore, since the class we are writing this in derives from Monobehaviour, we already have access to the FindObjectOfType() function without needing to prefix it with GameObject.

Second, the function FindObjectOfType() does not return a GameObject. You can see that the paddle variable does not have access to the functions of the GameObject class, such as AddComponent() or GetComponent(). What it does return, I think, is the first Component of the specified type it finds in the scene. And since components are always attached to GameObjects, you have indirect access to the GameObject’s transform (just like you do when you use this.transform.position in a Monobehaviour).

I feel like this fragment of explanation is still a bit confusing, so please ask for clarifications if needed. And if someone spots a mistake in my statements, please let me know too.

Hi,

Thanks for the answers! I think that this…

What it does return, I think, is the first Component of the specified type it finds in the scene. And since components are always attached to GameObjects, you have indirect access to the GameObject’s transform (just like you do when you use this.transform.position in a Monobehaviour).

…is indeed what is happening then. I was confused as to why I would get access to the transform of the GameObject if what I’m returning is the Paddle script component on the GameObject, but I guess that’s just what Unity allows you to do. Great stuff!

Thanks and good luck with your projects!

1 Like