FindObjectByType instead of FindObjectOfType

In the course “Complete C# Unity Game Developer 2D”, Section 3 “Snow Boarder”, video numer 42.

I’m finding difficulty on getting this FindObjectOfType working in Unity6, as it seems they replaced it with FindObjectByType. I’m not able to make it work with that new one.

I’m a little bit lost about this.

Hi lmgiorni,

Theoretically, you should get a warning/error message if you use FindObjectOfType in Unity 6 code. The error message tells you what the replacement is. If not, look for FindObjectOfType in the Unity API. There is a little hint on what you should use instead. Were you able to find it?

1 Like

Thanks, Nina, let me see if I can understand the difference, I’m not used to read programming documentation.

I made it work, it was simple, but I do not get how the () are put after the <> and then semicolons.

As SurfaceEffector2D() should be written like that.
Thought in my mind it should be this:
surfaceEffector2D = FindAnyObjectByType<SurfaceEffector2D()>;

instead of this:
surfaceEffector2D = FindAnyObjectByType<SurfaceEffector2D>();

But the last one is the one that works.

Thanks for any help you can give me on understanding such syntax.

It works fine replacing FindObjectOfType that is obsolete now, with FindAnyObjectByType.

Good job! I knew you’d be able to figure this out yourself. :slight_smile:

I made it work, it was simple, but I do not get how the () are put after the <> and then semicolons.

That’s fine. There is not much C# syntax to learn. Just look up some example, and try to apply the pattern as you did. If you do that a couple of times, you know C# (the programming language).

Method calls have the following pattern: DoSomething(). You already know a few examples such as Update(), Start() and Debug.Log().

The methods with <> are so-called generic methods. We always have Name<Type>(). <Type> comes directly after the name. The () always come at the end of a method to invoke the method. Name()<Type> is always wrong.

Later in this course, you’ll see List<QuestionSO>() where we generate a list of type QuestionSO to which we will be able to assign QuestionSO objects. It’s the same principle.

1 Like

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

Privacy & Terms