Type vs. Class Confusion

Seems to me that FindObjectOfType reacts to the line:

“public class Paddle : MonoBehaviour” at the top of Paddle.cs.

I created a Game Object called “Finder” and another called “Snitch” to test this. I added a script to Finder called Finder.cs, and in Finder.cs I use “GameObject.FindObjectOfType(chevron)Snitch(chevron)();”
(Aside: can’t type in chevrons in the forum?)

Then I get the error of finding nothing–so obviously a Snitch object in Unity isn’t enough. I create a Snitch.cs script, with the line “public class Snitch : MonoBehaviour” from creating it via Add Component (as opposed to public class NewBehaviourScript: MonoBehaviour"). And now Finder.cs has no trouble finding the Snitch. (Also tested: a component script of Snitch with the line “public class NewBehaviourScript” doesn’t get Finder to find the Snitch.)

Is FindObjectOfType looking for an instance of a class?

Isn’t there a difference between type and class?

Hi Samuel,

You can use chevrons, but because the forum supports BBCode, HTML and other forms of markup they need to be escaped. The easiest approach is to just format anything which is code accordingly with the prefix/suffix characters.

For a single line, I might consider just using the single ` character before and after, but for a block of code I would use three as prefixes/suffixes.

GameObject.FindObjectOfType<Snitch>();

private void Start()
{
    Snitch snitch = GameObject.FindObjectOfType<Snitch>();
}

Regarding your initial question, it doesn’t matter which way you add the Script components to a GameObject, either will work.

Object.FindObjectOfType<> will return the first object it finds in a scene which matches the specific type.

In the case of creating your own scripts, these need to be added to a GameObject in order for them to be found, they will not be found using the above if they are not added to a GameObject in the scene.

When you create a class you are defining your own custom type.

For example;

public class Person
{

}

In the above, I have just defined a class. When discussing objects, we would refer to this as an object of type Person.

So, describing your example above, we could say…

You defined two classes, Snitch and Finder, within two separate scripts. These were attached to GameObjects in the form of two separate Components.

You would be able to use GameObject.FindObjectOfType<Snitch>() or GameObjectOfType<Finder>() to get a reference to these objects within your scene.

Hope this helps. :slight_smile:


See also;

print ("Thank you this was very helpful.");

Just to make sure I get it–so a custom class that describes an Object is a type. Am I getting warmer?

However, I do have a Finder GameObject with a script command
GameObject.FindObjectOfType<Snitch>();"
that finds my Snitch script when it is not attached to any Snitch object.

The error shows up only when I change the line
public class Snitch : MonoBehaviour
to
public class anythingelse : MonoBehaviour

Maybe clever UnityEngine is allowing the FIndObject to find any public class//type in any script in the project?

33%20PM

An object is an instance of a class.

The GameObject doesn’t have to be named “Snitch” for it to find it, the GameObject could be called anything.

I can’t really see what is happening from the series of screenshots, might be easier to zip the project up and share it if you want me to take a look.

Scripts will only be executed when they are attached to GameObjects, so, unless your Snitch script is attached to a GameObject, Object.FindObjectOfType<Snitch>() won’t be returning anything, be it’s very definition, how can it return a GameObject if it wasn’t in the scene?

Ahh I think I see, partially. And I really appreciate your help.

A class is like a car, always has four wheels and a steering wheel. What it can do is defined by it’s inclusion in that class. And my car is an object in that class.

But I still don’t get what a “type” is and how that is different, if at all.

The reason I ask is that it seems like FindObjectOfType ought to be called FindObjectOfClass. When you change the Class name it’s searching for, it can’t find it anymore.

I zipped my test to show you how the scripts seem to be searching and being found without being attached to any GameObjects. If you just change the name of the public class in Snitch.cs from “notSnitch” to “Snitch” the error message will disappear.

finder_snitch.zip (132.7 KB)

In your analogy, you have defined something perhaps like this;

public class Car
{
    private int _numberOfWheels;
    private bool _hasSteeringWheel;

    public Car()
    {
        _numberOfWheels = 4;
        _hasSteeringWheel = true;
    }
}

The above is a C# example of your class (excluding the MonoBehaviour inheritance). Your class has defined a custom type, just like int or example.

Your type specifies that a Car will always have four wheels and a steering wheel.

If I wanted to also have a Robin Reliant, your class would not be suitable for me, despite being a Car, it has too many wheels. I would need to define a different type, or, perhaps consider re-writing your definition.

A class just defines a Type, so I would argue that it has been named correctly. You could also be using structs which are also types. :slight_smile:

I will take a look now.

Thank you. I’m going to reread this a few times this week. I think if I meditate on this and keep working with C#, eventually I can really internalize all these terms. Coding seems very straightforward, literal, and yet very deep. We learn to call up many things that have complex natures when we start from a tutorial, and it’s not necessary to know everything about every word at the outset, though it’s desirable to know it in the long run. I’m having a lot of fun stretching my brain with this course. :slight_smile:

And I’m excited to hear what you have to say about the strangeness in my test project.

Ok, so you have a couple of things going on here…

The error message is being generated by the compiler, it is unhappy because you are trying to use a type which doesn’t exist, e.g. when you say “Find A” but you don’t even have an “A” in the project. It would be like us meeting in a barn full of hay and me asking you to find the jibber-wockey, but not telling you what a jibber-wockey at is or looks like… you would just look at me like I was crazy - this error is the compiler doing that to you :wink:

The next thing I notice is that you have this;

// Use this for initialization
void Start () {
	GameObject.FindObjectOfType<Snitch>();
}

On it’s own, that is going to do very little, because what it does (or doesn’t find) isn’t be put anywhere.

Really, you’d want something like this;

// Use this for initialization
void Start () 
{
    Snitch snitch = GameObject.FindObjectOfType<Snitch>();
}

In the above, we define a local (to the method) variable named “snitch”, this will hold the reference to whatever the GameObject.FindObjectOfType<Snitch>() statement returns.

If we have a GameObject in the scene with our Snitch.cs class attached as as script component, happy days, snitch will reference that GameObject and we could perform other operations upon it.

If however we do not, then snitch is going to be null, if we were then to try and perform other operations upon it we would receive a nullreferenceexception error.

We could handle that with a couple of minor changes;

// Use this for initialization
void Start () 
{
    Snitch snitch = GameObject.FindObjectOfType<Snitch>();

    if(snitch)
    {
        // snitch returns true, thus we found something
        Debug.Log("Well done Harry! You got the Snitch!");
    }
    else
    {
        // snitch returns false, we found nothing
        Debug.Log("Dumbledore is displeased with your efforts Harry!");
    }
}

Hope this helps :slight_smile:

I’m having a lot of fun stretching my brain with this course.

Awesome :slight_smile:

All super helpful! Thanks so much!

1 Like

You are very welcome :slight_smile:

Privacy & Terms