The referenced script on this Behaviour is missing
The referenced script (Unknown) on this Behaviour is missing!
A GameObject or prefab has a component slot whose script Unity can no longer find. The Inspector shows it as Missing (Mono Script). The component's saved settings are still there, waiting for a script.
Read the Console line first
Recent Unity versions name the GameObject in the warning. Select it, and the broken component shows Script: Missing (Mono Script) in the Inspector.
Common causes
The script was deleted or renamed outside Unity
Unity tracks scripts by the GUID in their .meta file. Deleting or renaming the .cs file without its .meta breaks the link.
The script does not compile
While a script has compile errors, Unity cannot load its class, and objects using it show as missing until the errors are fixed.
An asset was imported without its scripts
A prefab or scene came from another project, but the scripts it uses did not.
The fix
// Editor helper: remove every missing-script component under the selection
using UnityEditor;
using UnityEngine;
public static class RemoveMissingScripts
{
[MenuItem("Tools/Remove Missing Scripts")]
static void Run()
{
foreach (var go in Selection.gameObjects)
GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go);
}
}Stop it coming back
Move and rename scripts inside the Unity Project window, which keeps the .meta file with them, and commit .meta files to version control.
Questions
- Why did renaming a script break every object that used it?
- Unity links each component to its script through the GUID stored in the script's
.metafile. Renaming or moving the file outside Unity, or deleting the .meta, gives the script a new GUID and the old links break. Rename and move scripts inside the Project window, which keeps the .meta file. - How do I find every object with a missing script?
- In an Editor script, loop over the scene's objects and call
GameObjectUtility.GetMonoBehavioursWithMissingScriptCount(go); any count above zero has a broken component.GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go)removes them once you are sure. - Will removing the missing component lose data?
- Yes, the values it saved are lost. If the script still exists under a different name or GUID, restore the original .meta file from version control first: the component reconnects and keeps its values.
Related errors
Got a different error? Paste it into the error lookup.