[Animator] Erases MaterialPropertyBlock: Restore Visuals
Under Audit
Unity 2022.3.x - Unity 6.1.x
Published Fri, Mar 20
Destroying a temporary Animator component can unexpectedly reset material properties previously set with a MaterialPropertyBlock, reverting them to default values even after the Animator is destroyed.
When a temporary Animator component is created to animate material properties, specifically when using a MaterialPropertyBlock, and subsequently destroyed, the animated material properties may unexpectedly revert to their default values. This behavior occurs even if the MaterialPropertyBlock is updated after the Animator is destroyed. In contrast, material color changes made directly via Material.color or Material.SetColor("_Color", ...) persist after the Animator is destroyed, though these methods force the creation of material instances. The issue specifically arises when a MaterialPropertyBlock is used to modify properties, leading to an unwanted reset of values (e.g., color reverting to white) instead of maintaining the last set or animated state. This necessitates storing and reapplying base colors, adding complexity to workflows that require immediate Animator destruction and subsequent material property updates.
- Store the material properties in a separate variable before destroying the
Animatorand reapply them usingMaterial.SetPropertyBlockafter destruction. - Instead of destroying the
Animatorimmediately, consider disabling its component or deactivating itsGameObjectto see if the property values persist. - Ensure that no other scripts or
Animationcurves are inadvertently resetting material properties after theAnimatoris destroyed.
Editor's Note:
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.