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?
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?
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.