an object is a class?

After looking at the unity documentation for a while and trying to study it, I found some parts somewhat confusing. This in particular “Base class for all objects Unity can reference”. I thought classes were like the “blueprints” of an object. So how can there be a class called object. Why would you say instantiate an object of the class object?

Unity’s Object class is the base class for unity objects, this is accomplished through inheritance.

The same is true for c# objects as well, but they use a different Microsoft object which differs from Unity’s Object class, but even Unity’s Object class derives from Microsoft’s Object class as all things do either directly or indirectly in c#.

Example, you create an Enemy class and inherit/derive from MonoBehavior.
Enemy class

So instead of implementing things from any of the classes in that stack of inheritance, you inherit the properties, methods, fields from all the base classes extending your Enemy class.

There are also times when you may not know the class that an object was instantaited from, you may use object to pass around that object and use various instructions to determine whether that type is something useful to you.

Because this is first how it is in .NET, everything inherits from object class, int, float, or classes, all have the few members from object (GetType, GetHashCode, ToString…).

Object with big O is the base class from Unity which also inherits from object (.NET).

Making everything inherit from a base class allows every single objects in the games to be treated the same. In this case, all objects have a name for instance and also a type Object.

This allows to retrieve all object doing:

FindObjectsOfType(typeof(Object));

Why would you instantiate an object of type Object? You probably would not, but you could create an Object reference that you want to use on a Transform then next on a GameObject and then on a custom class. Since they all inherits from Object it is possible:

Object o = gameObject;
print(o.ToString()) // print the name of the object
o = gameObject.rigidbody;
print(o.GetInstanceID()); // print the GUID of the rigidbody

this works but may be useless in most situations.

‘object’ -in the class- is just the name of the class. There is no relation between this name and Object in modern computer language which is a run-time instance of a class. It is just words similarity.

I think trying to understand classes and objects through Unity will only confuse you because it hides a lot of the details. I think if you read a little about classes, objects and inheritance and (more importantly) do some practice problems in a language X (C# makes it pretty straightforward in my opinion), it’ll become clearer. I’m new to Unity but not object oriented programming so I can’t really answer your question specifically.

My understanding though is that “Object” is referring to the specific instance of the class in question. So you write a script, you add it to a Game Object. You make that object a prefab that you can instantiate at will. When you instantiate it, it’ll have it’s own copy of all the variables and functions defined within it. The unattached script can be considered your class, the instantiated game object, your object.

So if you instantiate it and want to destroy it somewhere in the script (perhaps due to a Collider trigger), you can refer to that particular instance with

Destroy(this.gameObject);

Objects are instances of classes, and unless you’re creating derived classes, they’ll of course be instances of the base class. I hope that makes sense/helps clear things up a little.