UnityException: AddForce can only be called from the main thread.

I’m getting this weird error message:

UnityException: AddForce 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.
at (wrapper managed-to-native) UnityEngine.Rigidbody.AddForce_Injected(UnityEngine.Rigidbody,UnityEngine.Vector3&,UnityEngine.ForceMode)
at UnityEngine.Rigidbody.AddForce (UnityEngine.Vector3 force, UnityEngine.ForceMode mode) <0x405a8570 + 0x00062> in <5544884434d1400a98503330ada26cbd>:0
at UnityEngine.Rigidbody.AddForce (UnityEngine.Vector3 force) [0x00004] in C:\buildslave\unity\build\Runtime\Dynamics\ScriptBindings\Dynamics.bindings.cs:168
at Casablanca.Tank.OnCollisionStay (UnityEngine.Collision collision) [0x0003d] in C:\usr\dev\Unity\Juegos\Hover Raider\Hover Raider D\Assets\Casablanca\Scripts\Runtime\Tank.cs:194

I say is weird because it references OnCollisionStay which does call AddForce at line 194, but theoretically this is a method that is always called from the main thread.

I’m using the Job System to perform some calculations that are then used in the FixedUpdate loop. I’ve been working for several weeks without any problems, just few minutes before, I could play the game without any issues, I’ve been playing all day long as a matter of fact. But then I made a tiny change in the ForceMode and tried to play it again, it started correctly but then it suddenly frozen. It was totally unexpected as I just changed one parameter, but I wanted to be sure I didn’t broke something accidentally, so I went to VS and placed a break point at the beginning of FixedUpdate, I traced the program but it crashes when it tries to access Time.fixedDeltaTime, if I hover over fixedDeltaTime it appears an error tooltip saying: “Time.fixedDeltaTime can only be called from the main thread.” I went to the editor log to see if I could dig more and the file is stuffed with the message I pasted above.

All my jobs are Bust compiled… and like I said, I’ve been working with no issues for long time, the only new thing I tried today was that I added the [MethodImpl(MethodImplOptions.AggressiveInlining)] attribute to a couple of functions, don’t know if this can be a source of trouble. I’m also using the incremental compiler.

It seems it is a compiler error as the problem went away by re-importing the scripts.

I don’t see why you would be getting this error with the stack trace you supplied. We would need a repro case to investigate what is going on here exactly.

I understand that, but I made a mistake that will make very hard to reproduce the problem. I didn’t backup the project as it was before re-importing the scripts, unfortunately I realized too late that was a crucial step in tracing this problem, and now I can’t reproduce it. I guess under this circumstances there’s nothing much you can do other than keeping it in mind in case someone else experiences a similar issue.