UIToolkit StyleAnimation issue transition property references non-set value

So we’re seeing some weird issues, mostly on device (they are hard to trigger in editor)

It seems to have to do with the Style Animation system as that is what the stack trace gives us and it might be that an animation is triggered twice as seems to run into Dictionary key duplication:

System.ArgumentException: An item with the same key has already been added. Key: UnityEngine.UIElements.StylePropertyAnimationSystem+ElementPropertyPair
at System.Collections.Generic.Dictionary2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) [0x001cc] in <06a8633d56964cb4a0c4050a8ba5ed74>:0 at System.Collections.Generic.Dictionary2[TKey,TValue].Add (TKey key, TValue value) [0x00000] in <06a8633d56964cb4a0c4050a8ba5ed74>:0
at UnityEngine.UIElements.StylePropertyAnimationSystem+AnimationDataSet2[TTimingData,TStyleData].Add (UnityEngine.UIElements.VisualElement owner, UnityEngine.UIElements.StyleSheets.StylePropertyId prop, TTimingData timingData, TStyleData styleData) [0x00066] in /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/StylePropertyAnimationSystem.cs:112 at UnityEngine.UIElements.StylePropertyAnimationSystem+Values1[T].UpdateProgress (System.Int64 currentTimeMs) [0x000ae] in /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/StylePropertyAnimationSystem.cs:811
at UnityEngine.UIElements.StylePropertyAnimationSystem+Values`1[T].Update (System.Int64 currentTimeMs) [0x00008] in /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/StylePropertyAnimationSystem.cs:756
at UnityEngine.UIElements.StylePropertyAnimationSystem.Update () [0x0001d] in /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/StylePropertyAnimationSystem.cs:1431
at UnityEngine.UIElements.VisualElementAnimationSystem.Update () [0x000b1] in /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/VisualTreeAnimationUpdater.cs:87
at UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) [0x0001e] in /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/VisualTreeUpdater.cs:111
at UnityEngine.UIElements.Panel.UpdateAnimations () [0x0000d] in /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Panel.cs:957
at UnityEngine.UIElements.BaseVisualElementPanel.Update () [0x0001b] in /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Panel.cs:561
at UnityEngine.UIElements.RuntimePanel.Update () [0x0001d] in /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/GameObjects/RuntimePanel.cs:47
at UnityEngine.UIElements.UIElementsRuntimeUtility.UpdateRuntimePanels () [0x0001d] in /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/UIElementsRuntimeUtility.cs:218
at UnityEngine.UIElements.UIElementsRuntimeUtilityNative.UpdateRuntimePanels () [0x00001] in /home/bokken/buildslave/unity/build/Modules/UIElementsNative/UIElementsUtility.bindings.cs:26

How do we debug this or rather, is this a bug that should be cought by UIToolkit itself? Its really hard to reproduce reliably in Editor. This error also doesn’t go away, its stuck in this state perpetually it seems and UIToolkit is partially non-functional afterwards.

Managed a minimal repro that is consistent, it happens when you click a button that dismisses itself and the screen behind it but behind the button is another button that has a hover effect.

Its complicated. I’ll make sure to open a bug for this. Its really weird.

Ok after three hours of debugging this I now managed to cook it down to a minimal example and have submitted a bug report https://fogbugz.unity3d.com/default.asp?1413513:

It seems this is an issue that is related to a transition style definition that references a non set property such as:

.miau-button {
transition: background-color 250ms;
}

Where background-color is not set. This leads to a continuous stream of exceptions following a NullReferenceException on removal of an item that is not the top of its respective hierarchy.

Fun fact, this also only seems to happen if you add a VisualElement to the same hierarchy you are removing the one that contains the transition in the same frame!

Did you end up working around this? I think I have the same issue. Only in 2022.1.17 but not 2021 LTS

Yes, basically make sure you aren’t doing it. Or if you want to do it make sure to delay by one frame if you added something in the same frame.

HI, I met the same problem in Unity 2022.2.1f1. Is this issue fixed? Or I should always take care of the removing?

I’ve managed to hit it a couple of times in 2022.2.1. I can’t confirm it’s for the same reason, though.

I also encounter this error in 2022.2.1f1
In my case, the error randomly appears when you click on the GameObject (on which the UI Document is attached) in the scene hierarchy
but… As far as I can tell this doesn’t affect functionality… Just spamming bugs

We fixed

We fixed the issue in the latest Unity stream, I am asking for a backport in 2022.2.

2 Likes

This would be great – it’s difficult to work around.

Same error appearing in 2022.2.1f1 for me, since I’ve added this code:

            InventoryNotification.schedule.Execute(
                () => {
                    var x = InventoryNotification.style.scale.value.value.x;
                    x = Mathf.Clamp(x, 0.9f, 1.1f);
                    InventoryNotification.style.scale = new Scale(new Vector2(1 + (1 - x), 1 + (1 - x)));
                })
                .Every(1000);

and in the style:

... { ...
    scale: 1 1;
    transition-property: scale;
}

error starts spamming after leaving the playmode. stops spamming only after domain reload. which makes working with it a real pain.

it is preceeded by this two errors:

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UIElements.VisualElement.UpdateCallbackParentCategories () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement.get_eventCallbackParentCategories () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement.HasParentEventCallbacksOrDefaultActions (UnityEngine.UIElements.EventCategory eventCategory) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem+Values`1[T].SendTransitionCancelEvent (UnityEngine.UIElements.VisualElement ve, System.Int32 runningIndex, System.Int64 panelElapsedMs) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem+Values`1[T].CancelAllAnimations (UnityEngine.UIElements.VisualElement ve) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem.CancelAllAnimations (UnityEngine.UIElements.VisualElement owner) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement.UnityEngine.UIElements.IStylePropertyAnimations.CancelAllAnimations () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement.UnregisterRunningAnimations () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement.WillChangePanel (UnityEngine.UIElements.BaseVisualElementPanel destinationPanel) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement.SetPanel (UnityEngine.UIElements.BaseVisualElementPanel p) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement+Hierarchy.SetParent (UnityEngine.UIElements.VisualElement value) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement+Hierarchy.RemoveAt (System.Int32 index) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement+Hierarchy.Remove (UnityEngine.UIElements.VisualElement child) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement.RemoveFromHierarchy () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.UIDocumentList.RemoveFromListAndFromVisualTree (UnityEngine.UIElements.UIDocument uiDocument) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.PanelSettings.DetachUIDocument (UnityEngine.UIElements.UIDocument uiDocument) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.UIDocument.RemoveFromHierarchy () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.UIDocument.OnDisable () (at <469e18a818964f39910fa06bef052d82>:0)
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UIElements.VisualElement.UpdateCallbackParentCategories () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement.get_eventCallbackParentCategories () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElement.HasParentEventCallbacksOrDefaultActions (UnityEngine.UIElements.EventCategory eventCategory) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem+Values`1[T].QueueTransitionEndEvent (UnityEngine.UIElements.VisualElement ve, System.Int32 runningIndex) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem+Values`1[T].UpdateProgress (System.Int64 currentTimeMs) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem+Values`1[T].Update (System.Int64 currentTimeMs) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem.Update () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElementAnimationSystem.Update () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.Panel.UpdateAnimations () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.BaseVisualElementPanel.Update () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.RuntimePanel.Update () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.UIElementsRuntimeUtility.UpdateRuntimePanels () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.UIElementsRuntimeUtilityNative.UpdateRuntimePanels () (at <469e18a818964f39910fa06bef052d82>:0)

and then the error itself:

ArgumentException: An item with the same key has already been added. Key: UnityEngine.UIElements.StylePropertyAnimationSystem+ElementPropertyPair
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <8f06425e63004caf99a79845675f751e>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <8f06425e63004caf99a79845675f751e>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem+AnimationDataSet`2[TTimingData,TStyleData].Add (UnityEngine.UIElements.VisualElement owner, UnityEngine.UIElements.StyleSheets.StylePropertyId prop, TTimingData timingData, TStyleData styleData) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem+Values`1[T].UpdateProgress (System.Int64 currentTimeMs) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem+Values`1[T].Update (System.Int64 currentTimeMs) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.StylePropertyAnimationSystem.Update () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualElementAnimationSystem.Update () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.Panel.UpdateAnimations () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.BaseVisualElementPanel.Update () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.RuntimePanel.Update () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.UIElementsRuntimeUtility.UpdateRuntimePanels () (at <469e18a818964f39910fa06bef052d82>:0)
UnityEngine.UIElements.UIElementsRuntimeUtilityNative.UpdateRuntimePanels () (at <469e18a818964f39910fa06bef052d82>:0)

could fix it:

this throws the exception:

.foo {
    transition-property: scale;
    transition-duration: 1s;
    scale: 0.9 0.9;
}

.foo.enlarge {
    scale: 1.1 1.1;
}

this does not:

.foo {
    scale: 0.9 0.9;
    transition-property: scale;
    transition-duration: 1s;
}

.foo.enlarge {
    scale: 1.1 1.1;
}

also seemed to be related to: Unity Issue Tracker - [UIToolkit] ArgumentExceptions are thrown and spammed after transition with empty property (unity3d.com)

this is still an issue in 2022.2.10

You can track and vote for it right here:
Unity Issue Tracker - NullReferenceException and ArgumentException errors are thrown after entering and exiting Play mode when UI Document component is used in the Scene (unity3d.com)

I reported this bug. Since then 8 new versions have been released but it’s still not fixed. I’m using 2022.2.3f1 and waiting for the new 2022.2 in which it will be fixed. I can’t do more than one animation in a loop without it.

2 Likes

Please backport this! Still not fixed in 2022.2.11f1 which makes it so I can’t use animations in most of my UI, when it worked in 2021.3. This is a major regression!

I’ve filed a bug report too
https://unity3d.atlassian.net/servicedesk/customer/portal/2/IN-35324
It’s now very tricky to move on with my current project, built with 2022 (specifically to be able to use features of UITK), I’m uncomfortable moving the project to 2023 and being in bug-limbo, I’m considering stripping out all the uss transitions and replacing with DoTween

Apologies, I bumped the request for a backport.

1 Like

It seems like my issue reported as UUM-24716 is finally fixed in 2022.2.14 :slight_smile: The issue is gone. Check out your projects too.

2022.2.14

  • UI Toolkit: Fixed NullReferenceException followed by ArgumentException when removing an element from its panel during a style transition. (UUM-22564)
2 Likes