Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IgorAherne · Feb 01, 2017 at 01:18 PM · c#errorcompilation

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Feb 01, 2017 at 09:47 PM 0
Share

Where do you see your errors ? On the game itself (development build) ? Have you checked the output_log.txt inside the _Data folder ?

avatar image IgorAherne Hellium · Feb 01, 2017 at 10:46 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgorAherne · Feb 01, 2017 at 03:31 PM 0
Share

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:/

avatar image Bunny83 IgorAherne · Feb 01, 2017 at 06:04 PM 1
Share

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?

avatar image Bunny83 Bunny83 · Feb 06, 2017 at 06:01 PM 1
Share

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

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgorAherne · Feb 01, 2017 at 03:33 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

293 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges