- Home /
Console error is given without filename when compiling
Hello,
Upon compilation I am getting many errors like this:
FindObjectsOfType can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
The error explained what the mistake was, but there is no way to find out which file it's coming from..
I've literally used FindObjectsOfType
in hundreds of places, no way I can ctrl+F the solution.
Usually Debug console gives the filepath, like Core.get_cameraSplineMove () (at Assets/Gameplay Scripts and Assets/General Gameplay/Core.cs:177) but not this time.
Is there a way to navigate to the root of issue?
Where do you see your errors ? On the game itself (development build) ? Have you checked the output_log.txt inside the _Data folder ?
Hey) Happens during build and during editor-compilation (after I write something in my IDE, then return to unity)
After I did the proper build, Unity didn't generate any output_log
file in my _Data folder (switched from the Android to Standalone build, since file definitelly is not generated for Android).
Also checked the Editor Log, but it just perfor$$anonymous$$g compilation
on my scripts, then reloads assembly
. After that it produces the error, - meaning Unity compiles all files on c++ side (as I understand it), and only then bothers to output the error, once all is digested.
looked at this thread but didn't find anything in C:/users/UserName/AppData/LocalLow/CompanyName/ProductName
But its way of doing this is not helpful, - I can't easily find the file which caused the failure.
Answer by Bunny83 · Feb 01, 2017 at 02:47 PM
You most likely used "FindObjectsOfType" in a field initializer of one of your class. So something like this:
public class MyClass : MonoBehaviour
{
public YourType someVariable = FindObjectsOfType<YourType>();
}
This does not work as the fieldinitializers are called right before the constructor of the class. Since the classes are created on Unity's loading thread you get that error since you tried to use "FindObjectsOfType" on a seperate thread.
Well, i just said what the error has specifically told you.
Since the error comes from a different thread you don't get a stacktrace / filename.
I suggest you use your IDEs "find in files" feature and go through all occurrences of "FindObjectsOfType" and check where it's placed. If your IDE doesn't have such a feature, use another one ^^. Visual Studio as well as MonoDevelop do have such a feature.
edit
As mentioned in the comment you usually should get a stacktrace, even when executed from another thread, Maybe the code is in another assembly?
thanks man, but I've mentioned all of this in my question, wouldn't want to run find in IDEs, it would take ages. And there is most likely a possibility where function calls are nested in one-another. And as you've said, I would have to look through every occurance:/
Well, there's a little "hack" which you can use. Just implement a "wrapper" method for "FindObjectsOfType" and replace all calls to FindObjectsOfType with your wrapper method. That way you can actually execute your own code whenever somewhere your method is used.
Note: Since you can't use refactoring tool for this (as you can't "rename" the actual method) you have to use find&replace. So make sure that the name you replace is "unambiguous" so you can reasily replace it back. Also use "FineObjectsOfType<"
as search string.
Btw: when exactly do you get that error in Unity? If i do that on purpose i get a stacktrace. However i'm still on Unity version 5.3.6f1, though i don't think that should have changed. I just tried using it in the ISerializationCallbackReceiver callbacks (where you also can't use the Unity API) and i also get the same error with stacktrace. Even when i manually start my own thread i still get a stacktrace.
$$anonymous$$aybe you could add more background to your case? You said "upon compilation". So it most likely is a script that is attached to a object in the current scene or a prefab (any object where an instance is loaded / deserialized automatically). Try closing the current scene and stick with an empty scene. Does the error still happen after compilation?
If it shows up as an actual error you can enable the "pause on error" button on the console. It won't immediately stop, but as soon as this frame finishes.
The stacktrace might contain either a .ctor
(constructor) or a .cctor
(static constructor). I haven't really checked where the field initializer calls actually come from.
Ins$$anonymous$$d of using an external file you could log your stacktraces to the console. Unity's Debug.Log is thread-safe.
Another solution is to print your stuff using timestamps. Of course you would need to also print some logs inside Unity as reference. So you can narrow it down (after this time, before that time).
Answer by AurimasBlazulionis · Feb 01, 2017 at 02:18 PM
What you are looking for is code which is on separate threads. Look for files which have System.Threading
. Then find there FindObjectsOfType part and move it to the main thread. Also, if there is some FindObjectsOfType inside class or struct constructors, remove it.
thanks man, but I don't have any files which are using threading.
It's an issue with constructors, but constructors do function calls which are nested in one-another. Will be hard to follow each one until I find every single error. Also, I would have to look through every constructor :/
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
No Monobehaviour scripts in files 1 Answer
2-Dimensional Array Error [CLOSED] 1 Answer
Help I am getting an error CS0079 in my code and I don't know why. 1 Answer