- Home /
How to check if the compiler has an error.
Is there a way through C# to detect if the compiler receives a warning or an error? I am looking for an event based way, but alternatives would work. I don't necessarily need the cause of the error just the fact that an error occurred in the first place. I imagine it's buried in UnityEditor somewhere. Any help is appreciated.
Answer by Dave-Carlile · Jul 02, 2015 at 12:06 AM
You can register a log handler callback and intercept the log messages. That might give you what you want? The catch is that the logging threaded so anything you do there has to be thread safe...
Application.RegisterLogCallbackThreaded(LogHandler);
I use this to write log messages to my console...
void LogHandler(string logString, string stackTrace, LogType type)
{
lock (thisLock)
{
Console.WriteLine(logString);
LogInternal(logString, stackTrace, type);
}
}
Oh, as I write this there's a non-threaded version of this too - Application.RegisterLogCallback...
Hi Dave, I fiddled around with this for a bit and this is what I want but I am not sure how to implement it. I am using this for an editor extension to check if the user has received an error, but I am confused on how this works. Application.RegisterLogCallback in Unity 5 has become deprecated and replaced with Application.log$$anonymous$$essageReceived. This seems to be the same thing. However, since I don't intend on running the game and using this. Is there a way to use the delegate when the application isn't running. If that answer is no, it leaves me even more confused. What is the purpose of detecting errors in runtime, considering with fatals errors because the application cannot be run in the first place? To sum up a bit, I am looking for a way to check if an error occured on compile in the editor. Essentially as soon as the console sees it. Thanks for the help.
Hah, guess I hadn't noticed it was deprecated yet - I'll have to change that. :)
You should be able to hook up the event in a script that's runs during edit mode (by using the ExecuteInEdit$$anonymous$$ode attribute)
Hi Dave, after some work on it and frustration. I realized that my [ExecuteInEdit$$anonymous$$ode] attribute was in the wrong place. That is why I was having issues with it being called only in-game. While it seems to be buggy with fatal errors and exceptions. It works just fine with errors occuring during runtime, and that I can deal with. Thanks for your help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613201625im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
No Monobehaviour scripts in files 1 Answer
Compiler errors while switching to android 1 Answer
Problem with Mono compiler 0 Answers
Distribute terrain in zones 3 Answers
Unity Internal Compiler Error, not there in Editor 0 Answers