Calling a method with .GetComponent. Noob Question.

is this line of code calling a method.

playerControllerScript = GameObject.Find(“Player”).GetComponent();

How can that be when PlayerController is a class. Sry if i sound like an idiot
I was following a tutorial and this was mentioned because of the Curly brackets and semi colon.

1 Like

In your example, this line of code first calls static method “Find” of GameObject.

This method returns a GameObject (which can be null), and from the result the line calls GetComponent method requesting return type of PlayerController.

Internally GetComponent does something akin to iterating through all attached components, until one of desired found, and then returns it.

The value is then assigned to playerControllerScript.

So, which part do you have difficulty with?

I thought that PlayerController was being called as a method

like this…
PlayerController();

I was ignoring the angle brackets that take in PlayerController as a type. Hope that made sense. thanks.

Angle brackets means Generic functions or types are being used.

If you used “List” from System.Collections.Generic, then you should’ve seen “List”. This is similar.

Look up any source of information about C# and read about “generics”. For example:

1 Like

That’s called generics. Essentially, C# generics is a way to allow for types to be parameters when defining classes, interfaces, methods, and delegates, enabling more code reusability and type safety.

The GetComponent, in this case, is a generic method which returns a component of the requested type, if it is attached (otherwise null).

You can read more about generics here: