I have a very big project like a car racing game in Unity 4.6.7. Unfortunately after some minutes to hours running the game on iOS the screen suddenly turns completely blue. I tracked it down to the car position. It is suddenly not valid, its x,y,z has NaN value. The cars rigidbody has also the position NaN. The camera follows the car so it got corrupted, too.
On xcode it is not possible to step through, on desktop computers the problem does appear. I put code into Update(), LateUpdate() and FixedUpdate() which restores valid values from previous frame if NaN is detected. This did not work. At a couple of occurences the rigidbody is set to zero. Sometimes scenes get loaded additive and asychronously since it is an open world game.
I spent the better part of two days last week hunting for a bug that caused exactly the same problem in KSP. I would engage time warp (which sets Time.timescale) to 3x, and the game would immediately crash with loads of NaN errors.
Long story short though, I traced the issue to modifying Time.fixedTimeStep during play time. I commented out the line that modified it, and the crashes stopped.
Mind that this is on version 5.1.2 of Unity though, not sure if the same will apply to your case in 4.6.7, but might be worth a shot… These NaN issues are the worst type of bug to hunt down.
Thanks for the answers.
There is not write access to Time.fixedTimeStep in the code, but Time.timeScale is animated with values between 0 and 1. Disabling setting the time scale doesn’t fix my problem.
I figured out when I deactivate the (very long and complex) wheel physics script completely the problem does not happen any more. I made lots of debug code around all write access to transforms, rigidbodys and even many lines around float divisions. But nothing suspicious so far! I think a division by zero will fire an exception in the debug ouput in any case? But there is no exception on Mac and Windows and not in xcode debug output. All forces applied to the rigidbody looks reasonable. All physics related code is done in FixedUpdate, not Update or LateUpdate.
I start feeling having some error in Unity or xcode.
I would bet that you have a division over zero X/0 = infinite, so MAYBE, just maybe you are doing an int/float, where float can be 0.01 but since it is being taken as an int/float it is int/0
Zero divs with floats and doubles unfortunately won’t throw any exceptions (we have the IEEE 754 standard to blame for that). Zero div with ints or decimals will though.
I went as far as writing a wrapper class for doubles and floats here, to overload their operators and throw exceptions, and replaced every float and double in every script that might have been even remotely related to rigidbody operations. I found a few cases where I had zero divisions and wasn’t aware, but none of them were the cause of this particular issue.
I definitely agree we’re looking at a unity bug here. Even more so because this issue appeared after upgrading to a new unity version (5.x). The code I suspected hadn’t been modified, and worked just fine in previous versions (4.6.x and earlier).
I’m afraid I can’t offer much more advice here, other than suggesting you comment out even your reads to Time.fixedDeltaTime and friends.
Best of luck with your hunting, and please let us know what you find
and somewhere between some minutes and some hours it happened that the result was NaN. The following chain of desaster was not obvious and took me over a week to track it backwards with lots of debug output. The correction is here:
Can’t tell you how many times this has happened to me in my own engine over the years. Divide by zeros are the devil, but you can’t really do a lot without division. If I see a chance of there being a division by zero ever occurring somewhere, I’ll wrap it up in an “if” statement to deal with the 0 case.
Just as a side note, my hardkour game increases/decrease timescale without any physics problems. Granted the physics are simple. Works fine in U4 and U5.