I have made a script somewhat similar to this one: http://answers.unity3d.com/questions/5020/searching-a-project-for-missing-mono-script
But rather than just finding the null entries, I simply want to sweep through and delete them all. By that I mean remove the null references from the GameObjects's list of Components.
The documentation for GameObject.AddComponent notes that there is no such thing as RemoveComponent, requiring you to call Object.Destroy() to remove that entry. This is impossible because there is no object to Destroy.
Creating a new GameObject and adding the components of the old one to it would invalidate any references to it.
Is there a way to do this without laboriously clicking through the UI?
NEW EDIT: (in light of comments below)
Are these references to some funny objects representing a missing script that the overloaded == operator is treating as equal to null? No, because calling DestroyImmediate() does nothing, and trying to actually de-reference the pointer causes a null reference exception. It must be that there are null pointer entries in the list, and the list is not modifiable by editor script.
Here is the demonstration code:
I believe there is currently no easy solution to do this in scripting. The internal implementation when you pick "Remove Component" from the popup menu is in C++, where all MonoBehaviour components are just belonging to a MonoBehaviour class. Once these are passed to C#, they get the class types of their scripts, or, if the script is empty, then null, which cannot be used to destroy the object.
You might be able to get a pointer reference to the actual MonoBehaviour component which you could call delete on, by iterating through all the GameObjects properties using the SerializedObject class, but that probably takes same time to get to work, if it is possible at all.
answered Jun 28 '10 at 08:50 AM
jonas echterhoff ♦♦
I just like to add that if you have Unity pro you can use the yaml text format for scene files. This allows you to edit the scene in a text editor. However it's still difficult to identify MonoBehaviours with missing components since the component just has a guid field which identifies which script asset was used by this MonoBehaviour.
I think it should be possible to make an editor script that searches for all MonoBehaviours in the scene file and try to match them up with the actual components in Unity. That way you should be able to find the component in the text file and delete it from the scene file manually.
answered Feb 22 at 03:18 PM
If calling Destroy isn't working on them, and they are actually a missing reference that is reporting null, could you just try setting the variable to null explicitly to get rid of the missing references? They should be cleaned up by mono if they are storing anything if you're worried about memory leaks.
answered Jun 11 '10 at 10:56 PM
When you create the object, just be sure to use Object.hideFlags = HideFlags.DontSave;
assuming that is you're creating it program-maticaly.. otherwise, there isn't much you can do if the object is null. it will be cached for later referencing.
answered Jul 11 '10 at 04:28 AM