Documentation

Unity Engine

Download Unity

Unity Engine

Object base class in Unity

Understand how UnityEngine.Object works as the base class for objects that the Unity Editor can reference, and how its lifetime and null checks differ from regular C# objects.
Read time 4 minutesLast updated 2 days ago

The
UnityEngine.Object
class acts as a base class for all objects that Unity can reference in the Unity Editor. You can drag classes that inherit from
UnityEngine.Object
into fields in the Inspector, or select them with the
Object Selector
next to an Object field.

An example of an Object Field in the Inspector window. The Object Selector is the circular icon to the right of the field.

Choose a base class

Rather than inheriting directly from
Object
for your own custom classes, it's often better to inherit from a class designed to be more specific to your goal. For example:
  • Inherit from
    MonoBehaviour
    if you want to write a custom component that you can add to a GameObject, to control what the GameObject does or provide some functionality relating to it.
  • Inherit from
    ScriptableObject
    if you want to create custom assets that can store serialized data.
Both of these inherit from
UnityEngine.Object
but provide extra functionality to suit those purposes.
Note
UnityEngine.Object
is different from .NET's base
System.Object
.
System.Object
isn't included in the default script template to avoid name conflicts. If you want to create classes that you don't need to assign in the Inspector, inherit from .NET's
System.Object
.
The
Object
class provides methods for instantiating and destroying Objects and for finding references to Objects of a specific type. For more information on the API for the Object class, refer to the script reference page for Object.

Special behavior of UnityEngine.Object

UnityEngine.Object
is a special type of C# object in Unity, because it's linked to an unmanaged (C++) counterpart object. For example, when you use a Camera component, Unity stores the state of the Object on the Object's C++ counterpart, not on the C# Object itself.

Two containers side-by-side represent the unmanaged and managed layers of the Unity Engine. Each container has its own representation of UnityEngine.Object. Connecting lines between the two layers demonstrate the link between the managed Object instance and its unmanaged counterpart.

Unity doesn't currently support the use of the C#
WeakReference
class with instances of
UnityEngine.Object
. For this reason, you shouldn't use a
WeakReference
to reference a loaded asset. Refer to Microsoft's WeakReference documentation for more information on the
WeakReference
class.

Unity C# and Unity C++ share UnityEngine Objects

When you use a method such as Object.Destroy or Object.DestroyImmediate to destroy an object derived from
UnityEngine.Object
, Unity destroys (unloads) the C++ counterpart object. You can't destroy the C# object with an explicit call, because the garbage collector manages the memory. Once there are no longer any references to the managed object, the garbage collector collects and destroys it. If your application tries to access the destroyed object again, Unity treats it as null.
Destroying an object is different from unloading an asset. When Unity unloads an asset, for example through
Resources.UnloadAsset
or
Resources.UnloadUnusedAssets
, the asset still exists on disk. If your application accesses an unloaded asset through an existing reference, Unity reloads its C++ counterpart from disk for most types. The exceptions are
MonoBehaviour
and
ScriptableObject
instances, which Unity doesn't reload through an existing reference.

Custom equality operators

For types that inherit from
UnityEngine.Object
, Unity uses a custom version of the C# equality and inequality operators. This means the null check
myGameObject == null
can evaluate
true
(and conversely
myGameObject != null
can evaluate
false
) even if
myGameObject
technically holds a valid C# object reference. This happens in two cases:
  • The object can be a fake null or placeholder object which Unity uses in the Editor only to populate uninitialized MonoBehaviour fields. These objects store useful debugging information to help you locate the source of these fields if you try to reference them.
  • The object can be a managed (C#) object which hasn't yet been garbage collected but that Unity considers null because the unmanaged (C++) counterpart object is destroyed.
Because you can't overload the
??
and
?.
operators, they're not compatible with objects that derive from
UnityEngine.Object
. The operators don't return the same results as the equality and inequality operators when you use them on a destroyed
MonoBehaviour
or
ScriptableObject
while the managed object still exists.

Additional resources