The comment in the documentation here:
Means that you canāt add it via the inspector. Unity when serializing your scenes/prefabs lookup your scripts via a guid, that guid is stored inside a meta file along with the script file.
For example. Iāve create a scene with 2 GameObjects in it:

The MyTestGameObject has a single script on it called zTest03:

Here you can see that script has a meta file:

The contents of the meta file is this:
fileFormatVersion: 2
guid: 7efa508250b1b1148b7fd26533435563
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Note that guid. Thatās the unique identifier for this file.
Now when we go and look at the *.unity file for this scene weāll find the GameObject in question:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!29 &1
####
.... SNIPPED A MAJOR PORTION OF YAML UNRELATED ...
####
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2096099674}
- component: {fileID: 2096099673}
m_Layer: 0
m_Name: MyTestGameObject
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &2096099673
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2096099672}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7efa508250b1b1148b7fd26533435563, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &2096099674
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2096099672}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -5.313465, y: 3.7368402, z: -2.9684873}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
In it you can see 3 things⦠The GameObject, a MonoBehaviour, and a Transform.
The MonoBehaviour is the serialized representation of our script.
And at this line:
m_Script: {fileID: 11500000, guid: 7efa508250b1b1148b7fd26533435563, type: 3}
You can see our previous guid:
7efa508250b1b1148b7fd26533435563
This is super helpful for many reaons:
- you can easily rename files since the guid doesnāt change. The rename just also renames the meta file.
- you can have 2 scripts with the same name (just in different namespaces so C# doesnāt freak out, and different folders so your FileSystem doesnāt freak out). This is helpful for if you import 3rd party libraries say from the asset store
- it just makes for easy/fast lookup by the editor/serialization engine
- more reasons Iām likely unaware of on the internal side of things
There is an exception to this rule though⦠and that is scripts found within dllās. And this is actually where the āfileIDā gets used. If your script is inside a dll, the guid will point to the meta file associated with the dll. And the fileID will be a hash associated with the script located inside that dll. Unity has created code to deal with this specific edge case, and itās fairly trivial since they can rely on the various .net/mono tools for managing dllās to extract that info.
Where as doing so for a random *.cs file that happens to have 2 scripts written in it⦠not as simple to do. Doable⦠in theory sure. But why support it other than to allow people to create more difficult to manage code?
Note even the dll thing is annoying because if you happen to delete that dll (say to import a new version of it), and that meta file gets deleted. Well⦠you just lost reference to ALL the scripts in that dll and the editor wonāt know how to find them. This even goes for if you rename the class itself within the dll⦠makes developing a dll super annoying. Itās why I no longer use them for my libs.
ā¦
Mind you this doesnāt stop you from adding said nested MonoBehaviour at runtime via āAddComponentā.
For example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class zTest03 : MonoBehaviour
{
private void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
this.gameObject.AddComponent<Blargh>();
}
}
private class Blargh : MonoBehaviour
{
private void Start()
{
Debug.Log("Blargh: " + this.gameObject.GetComponents<MonoBehaviour>().Length);
}
}
}
This will work fine because the runtime doesnāt care. Iāve actually done this in games Iāve released as ways to attach little tags to gameobjects for various things.
The whole asset guid thing is a editor/inspector/serializer dealio. You canāt attach the script via the editor and have the build pipeline recognize it.