Unity has a class in the UnityEngine
namespace called Object
, which acts as a base class for all objects that Unity can reference in the Unity Editor. You can drag and drop classes that inherit from UnityEngine.Object
into fields in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary, or pick them using the Object Picker next to an Object field.
This section provides an overview of the Object class and its common uses when scripting with it. For a complete reference of every member of the Object class, refer to the Object script reference.
When creating your own objects via scripting, you typically don’t want to inherit directly from Object
. Instead, inherit from a class designed to be more specific to your goal. For example:
MonoBehaviour
if you want to write a custom component which you can add to a GameObject
, to control what the GameObject
does or provide some functionality relating to it.ScriptableObject
if you want to create custom assets which can store serialized data.Both of these inherit from UnityEngine.Object
but provide extra functionality to suit those purposes.
Note: Unity’s
Object
class is in theUnityEngine
namespace. This is different from .NET’s baseObject
class, which has the same name, but is theSystem
namespace, and is not included in the default script template, so that the names don’t clash. You can still inherit your classes from .NET’sSystem.Object
if you want to create classes in your script which you don’t need to assign in the Inspector.
Unity’s Object
class acts as the base class for most of Unity’s built-in classes such as GameObject
, Component
, Material
, Texture
, Mesh
, Sprite
, and many more. You can drag and drop all these types into reference fields in the Inspector.
If a field in the Inspector specifies a type of class (such as texture), you can only drop objects of that type into the field and the object picker only displays objects of that type.
The previous image shows three types of object field in the Inspector.
The first is of type Object, meaning you can assign any Unity Object to this field, such as a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary, a texture, an audio clipA container for audio data in Unity. Unity supports mono, stereo and multichannel audio assets (up to eight channels). Unity can import .aif, .wav, .mp3, and .ogg audio file format, and .xm, .mod, .it, and .s3m tracker module formats. More info
See in Glossary, or anything else. This isn’t usually useful, and it’s better to make your fields be more specific about what they accept.
The second shows that its type is Texture
, as shown in the parentheses. Texture is a built-in Unity class, and this means you can drop any texture Asset into this field. Unity has two classes which inherit from this, Texture2D
and RenderTexture
, which means you can drop either of these types into this field.
The third shows that its type is Food
. There’s no built-in Unity class with this name, so this example is showing a custom user-made class which inherits from Object
. If you create classes which inherit from Food
, such as Apple
and Banana
, you can assign references to instances of these classes into the Food
field, because they inherit from that type.
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, srefer to the script reference page for Object.
UnityEngine.Object
is a special type of C# object in Unity, because it’s linked to a native (C++) counterpart object. For example, when you use a CameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary component, Unity stores the state of the Object on the Object’s native C++ counterpart, not on the C# Object itself.
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.
When you use a method such as Object.Destroy or Object.DestroyImmediate to destroy an object derived from UnityEngine.Object
, Unity destroys (unloads) the native 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 a destroyed UnityEngine.Object
again, Unity recreates the native counterpart object for most types. Two exceptions to this recreation behavior are MonoBehaviours and ScriptableObjects: Unity never reloads them once they have been destroyed.
MonoBehaviour
and ScriptableObject
override the equality (==) and inequality (!=) operators. If you compare a destroyed MonoBehaviour
or ScriptableObject
against null
, the operators return true when the managed object still exists and hasn’t yet been garbage collected.
Because you can’t overload the ?? and ?. operators, they aren’t 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.
Object