Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Unable to see missing scripts via GUI on certain Prefabs Models

Discussion in 'Scripting' started by Virtumonde, Dec 4, 2020.

  1. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
    Hi! I am using an old scene from 2017. It had a couple of old, unsupported light features here and there but I managed to make it work. Now, upon attempting to make a Prefab from a Prefab Model (by dragging it into a folder), I get the following error:
    Code (CSharp):
    1. You are trying to replace or create a Prefab from the instance 'rock_small' that references a missing script. This is not allowed.
    2. Please change the script or remove it from the GameObject.
    3. UnityEditorInternal.InternalEditorUtility:ProjectWindowDrag(HierarchyProperty, Boolean)
    4. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
    This suggests that there is at least a missing script in the Prefab Model. Further, I confirmed that around 40% of all the Prefab Models in the scene have at least a missing script using this code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections.Generic;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class SelectGameObjectsWithMissingScripts : Editor
    7. {
    8.     [MenuItem("Tools/WPAG Utilities/Select GameObjects With Missing Scripts")]
    9.     static void SelectGameObjects()
    10.     {
    11.         //Get the current scene and all top-level GameObjects in the scene hierarchy
    12.         Scene currentScene = SceneManager.GetActiveScene();
    13.         GameObject[] rootObjects = FindObjectsOfType<GameObject>();
    14.  
    15.         List<Object> objectsWithDeadLinks = new List<Object>();
    16.         foreach (GameObject g in rootObjects)
    17.         {
    18.             //Get all components on the GameObject, then loop through them
    19.             Component[] components = g.GetComponents<Component>();
    20.             for (int i = 0; i < components.Length; i++)
    21.             {
    22.                 Component currentComponent = components[i];
    23.  
    24.                 //If the component is null, that means it's a missing script!
    25.                 if (currentComponent == null)
    26.                 {
    27.                     //Add the sinner to our naughty-list
    28.                     objectsWithDeadLinks.Add(g);
    29.                     Selection.activeGameObject = g;
    30.                     Debug.Log(g + " has a missing script!");
    31.                     break;
    32.                 }
    33.             }
    34.         }
    35.         if (objectsWithDeadLinks.Count > 0)
    36.         {
    37.             //Set the selection in the editor
    38.             Selection.objects = objectsWithDeadLinks.ToArray();
    39.         }
    40.         else
    41.         {
    42.             Debug.Log("No GameObjects in '" + currentScene.name + "' have missing scripts! Yay!");
    43.         }
    44.     }
    45. }
    Provided that I cannot see the missing scripts, how do I delete them?
     
  2. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
    Update 1:
    Using
    Code (CSharp):
    1. DestroyImmediate(currentComponent);
    on every missing component didn't work
     
  3. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
  4. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
  5. kural_varman

    kural_varman

    Joined:
    Jan 6, 2023
    Posts:
    2
    it will nor work as the component is null
     
  6. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Nice necro of a 2+-year old post.

    Here's what you should do, if anyone comes across this from Google:
    UnityEditor.GameObjectUtility.RemoveMonoBehavioursWithMissingScript(gameObject);