Force compile error on missing script
Hi, Is there a way to force a compile error if there's some Gameobject / Prefab with a missing script?
Answer by awsapps · Aug 11, 2021 at 08:27 AM
Thanks rage_co! The post Callback after scripts finish compiling you mentioned is sufficient for what I want.
This should be a workflow for deleting a monobehaviour safely:
You want to delete the monobehaviour A.
Before deleting the script, add the method:
[UnityEditor.Callbacks.DidReloadScripts] private static void OnScriptsReloaded() { System.Type monobehaviourType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType; Object[] objs = GameObject.FindObjectsOfTypeAll(monobehaviourType); foreach (Object obj in objs) { Debug.LogWarning($" Watch out! The monobehaviour {monobehaviourType} is currently being used by {obj.name}", obj); } }
Focusing on the editor again, the console will snitch all GameObjects (including prefabs) that are using such script. Double clicking each warning line will redirect to each GO.
Marking your reply as accepted. Thanks!
glad you found a solution, and this is quite useful so i might use it sometime later in the future too, so a thanks in advance!!
Answer by rage_co · Aug 11, 2021 at 06:15 AM
It's really simple to do this at runtime....for example let's say we want to give a compiler error if a certain component.... let's say CameraManager is missing...
if(target.GetComponent<CameraManager>() == null)
{
Debug.LogError("Component missing!");
}
Debug.LogError basically gives an error....hope this helps
Answer by awsapps · Aug 11, 2021 at 06:30 AM
Not in runtime?
Im not too sure in that case...maybe Editor scripting? But since i haven't had any experience with it i can't really say
I found this question and it might contain what you need "Callback after scripts finish compiling"...
Also, just a piece of advice that you should use Debug.LogWarning instead of LogError since if you give out an error each time....you will not be able to do anything in the editor and actually fix the error...so a Warning should do just fine
Your answer

Follow this Question
Related Questions
error CS0118 'charanim.CameraTurn' is a field but a 'type' was 'expected' 1 Answer
why do i keep getting compiler errors even though my script has no errors? 2 Answers
DontDestroyOnLoad not compiling in Visual Studio 2 Answers
VS Code showing errors in Editor scripts while referencing another class 1 Answer
Multiple errors after updating asset package from store 0 Answers