Attaching new script to a "missing script" component.

Hello! I’m converting HDRP project to URP and by removing HDRP package it deleted some scripts from a lot of my prefabs. I would like to somehow attach another script in those null components. Is it possible? I’ve tried writing some editor scripts to do that for me but the main problem was that the changes weren’t applied to prefabs.

Here is an example of one of those scripts:

[MenuItem("Component/Find missing scripts")]
    public static void FindMissing()
    {
        string[] prefabPaths = AssetDatabase.GetAllAssetPaths().Where(path => path.EndsWith(".prefab", System.StringComparison.OrdinalIgnoreCase)).ToArray();

        foreach(string path in prefabPaths)
        {
            GameObject prefab = PrefabUtility.LoadPrefabContents(path);
            Component[] components = prefab.GetComponents<Component>();
            Component[] childComponents = prefab.GetComponentsInChildren<Component>(true);

            foreach(Component component in components)
            {
                if(component != null && component.name.ToLower().Contains("decal"))
                {
                    component.gameObject.AddComponent<DecalProjector>();
                }
            }

            foreach (Component component in childComponents)
            {
                if (component != null && component.name.ToLower().Contains("decal"))
                {
                    component.gameObject.AddComponent<DecalProjector>();
                }
            }
        }
    }

You probably need to use Undo.AddComponent() Unity - Scripting API: Undo.AddComponent
to make sure the changes are saved.

Okay, I’ll try to do this with Undo and let you know if it worked for me.