Fixing the “Referenced script on this Behavior is missing” issue in Unity
tldr; version – Jump to the end of this post to download a Unity Extension that will help you resolve “Missing Script” problems. The video below will show how it’s used.
Anyone who has been using Unity for very long has encountered the dreaded “Referenced script on this Behaviour is missing” problem, which typically occurs either when you have a Component on one of your objects whose script has been deleted or you have moved a scene from one project to another and even though they both have the appropriate scripts, the serialized reference to the script is no longer valid.
I have personally encountered this dozens of times, and usually it just happens with one or two objects in a scene and isn’t a major headache to deal with. Recently, however, I’ve been working on building a free redistributable version of DF-GUI™ for people to try it out, and in the process of moving the example scenes to a new project Unity seems to have lost the references to every script on every component in every scene. Imagine the horror. I almost gave up on making a free version right then and there, as there was no way in hell I was willing to fix up each of those references “by hand” so to speak.
I’ve been down this road before, and knew very well what a quick Google search would reveal: There is probably no more frequently asked question than “how do I fix the ‘Missing Script’ problem?” and its variants. There are easily dozens of questions on the Unity Answers site for this topic alone, usually answered with a brief explanation of why the problem occurred followed by instructions on how to manually re-assign the scripts.
Not being one to give up easily, I couldn’t be satisfied with that. I knew there must be a way to fix this that didn’t make my eyes bleed. After all, the Unity Editor obviously still had all of the saved data for the component and displayed it in the Inspector and I knew that there must be some way to make use of that, so I kept searching and eventually found an interesting post on the UnityGems site about the issue that actually had a solution. Well, sort of.
That site had a UnityPackage that I could download which would scan the scene and compare components with missing scripts to the scripts in your project and attempt to match them up. Brilliant! Unfortunately, what I discovered when I ran it is that it rips through your scene at Mach 5 and auto-assigns what it thinks is the most likely script (assuming it finds only one likely match) without any action on your part. That might be incredibly helpful if every instance of the missing script refers to the same script, and there’s a very high likelihood of the component finding the correct match. That was the scenario posited in the post, and I’m sure it works very well for that.
For me, however, it was a disaster. It was, in fact, worse than the original situation in the sense that I now had the wrong script assigned to several objects and no way to automatically detect this unfortunate situation. Unlike missing scripts, it’s not easy to detect if the *wrong* script is assigned to a component. I decided that I wanted to manually review each component to make sure the correct script was assigned, I just needed a tool to detect the missing script and provide the best suggestions. Inspired by the UnityGems blog post, I wrote my own Editor utility from scratch.
This version allows you to find components with missing scripts either in the current scene or in any prefab in your project. If you select a component with a missing script, the custom Inspector will provide you with a list of possible scripts to assign based on comparing the serialized properties of the component against the fields defined in all scripts available to your project (including those in referenced DLL assemblies). Simply click the button of one of these scripts to assign it to the component.
This ended up saving me a great deal of time, and I even managed to retain most of what’s left of my hair. I hope this helps you as well.
Here’s a very brief video description and overview of how to use it:
PLEASE NOTE: The latest version of this script available for download now shows up in the Window menu instead of a top-level Daikon Forge menu, in order to be more consistent with Unity Technology’s plugin guidelines.