• Unity
  • NaN error within Spine code

Hey!
Recently I got this error when playing Spine animation which happens every frame:

ArithmeticException: Function does not accept floating point Not-a-Number values.
  at System.Math.Sign (System.Single value) [0x00028] in <2b3a3162be434770b7a4fac8b896e90c>:0 
  at Spine.AnimationState.ApplyRotateTimeline (Spine.RotateTimeline timeline, Spine.Skeleton skeleton, System.Single time, System.Single alpha, Spine.MixBlend blend, System.Single[] timelinesRotation, System.Int32 i, System.Boolean firstFrame) [0x0017a] in <235d56ff74434cbc92c60912dbb8d9f4>:0 
  at Spine.AnimationState.Apply (Spine.Skeleton skeleton) [0x0025a] in <235d56ff74434cbc92c60912dbb8d9f4>:0 
  at Spine.Unity.SkeletonAnimation.ApplyAnimation () [0x0002b] in <4498d6c5ece742429fe17167eb56a8b8>:0 
  at Spine.Unity.SkeletonAnimation.Update (System.Single deltaTime) [0x0005c] in <4498d6c5ece742429fe17167eb56a8b8>:0 
  at Spine.Unity.SkeletonAnimation.Update () [0x00028] in <4498d6c5ece742429fe17167eb56a8b8>:0 

I have a bumper that has idle animation on layer 0.
Also it has a "push_middle" animation on layer 1. It plays one time after the bumper is being hit by something. The animation only has it's position animated and not rotation. Alpha of this layer depends on the git strength.
Also on layer 2 I have "push_left" and "push_right" animations that play if the bumper is being hit on one of the edges. These animations only apply rotation. The alpha on this layer is affected by strength and angle of the hit.
Strength is clamped between 0.1 and 2, and angle is clamped between 0 and 1.
Very rarely the bumper gets hit and dissappears as if it was hit way too hard and got pushed into a stratosphere. And after that this error pops up every frame.

I understand there's some issue in my code but the bug is very hard to reproduce. So maybe someone could give me a hint of what could possibly causing that error.

Related Discussions
...
  • Изменено

I'm sorry to hear you're having trouble. Our Unity expert, Harald is on vacation at the moment. Could you please send us a minimal Unity project that can reproduce the problem via email, so that he can quickly identify the problem when he comes back?: contact@esotericsoftware.com
Please briefly mention this forum thread URL in the email so that we know the context. Then we can have a look at what's going wrong.

4 дня спустя
OverBobin написал

I understand there's some issue in my code but the bug is very hard to reproduce. So maybe someone could give me a hint of what could possibly causing that error.

It's very hard to say without a reproduction project, and since the exception above does not list line numbers. Could you only reproduce this on the target device and not within the Unity Editor in Play mode?

Are you playing back animations normally via setting them at AnimationState and nothing else, or do you use some other game logic to modify e.g. bone positions via e.g. Unity physics? If so, my guess would be that some Transform position or rotation values that are used for bone location become NaN and are then transferred to the skeleton (via a SkeletonUtilityBone in mode Override), leading to subsequent issues at a later stage.

Thanks for the replies!
Unfortunately it is almost impossible to reproduce consistently. I haven't encountered it personally, this error was provided by a QA team.
I don't use any other logic, just multipel animation layers with alpha values that are dynamically changed.

Which version of the spine-unity runtime (name of the unitypackage, also listed in Assets/Spine/version.txt) are you using?

9 дней спустя

Package version is spine-unity-4.1-2022-07-14

Thanks for the info. Unfortunately the version is new enough so that there have not been any related bugfixes afterwards. I'm afraid we would need a reproduction project in order to find out the cause of this issue, even if it's only occurring in one of a thousand test iterations.

10 месяцев спустя

I also encountered this problem recently. The reason is that we set timeScale to an infinite value or a NaN value. Both cases may eventually lead to this error of yours and other errors as follows:

IndexOutOfRangeException: Index was outside the bounds of the array.
Spine.Animation.BinarySearch (System.Single[] values, System.Single target, System.Int32 step) (at Assets/Framework/Plugins/Spine/Runtime/spine-csharp/Animation.cs:100)
Spine.TranslateTimeline.Apply (Spine.Skeleton skeleton, System.Single lastTime, System.Single time, Spine.ExposedList`1[T] firedEvents, System.Single alpha, Spine.MixBlend blend, Spine.MixDirection direction) (at Assets/Framework/Plugins/Spine/Runtime/spine-csharp/Animation.cs:477) 

or

IndexOutOfRangeException: Index was outside the bounds of the array.
Spine.Animation.BinarySearch (System.Single[] values, System.Single target) (at Assets/Framework/Plugins/Spine/Runtime/spine-csharp/Animation.cs:116)
Spine.AnimationState.ApplyAttachmentTimeline (Spine.AttachmentTimeline timeline, Spine.Skeleton skeleton, System.Single time, Spine.MixBlend blend, System.Boolean attachments) (at Assets/Framework/Plugins/Spine/Runtime/spine-csharp/AnimationState.cs:483)
Spine.AnimationState.Apply (Spine.Skeleton skeleton) (at Assets/Framework/Plugins/Spine/Runtime/spine-csharp/AnimationState.cs:255)

Infinity may be caused by division by 0.

NaN may be caused by 0 divided by 0, 0 multiplied by infinity, or it may be caused by trying to convert a non-numeric string to a float. For details, please refer to this wiki:
https://en.wikipedia.org/wiki/NaN#Operations_generating_NaN

I am writing here to share my experience, hoping it will be helpful to others in the future.

@zhong317 Thanks for sharing!