Search Unity

Question OnValidate() not working with animation

Discussion in 'Animation' started by tanmaykulkarni, Jan 24, 2023.

  1. tanmaykulkarni

    tanmaykulkarni

    Joined:
    Nov 5, 2019
    Posts:
    104
    Hey Devs,
    I have a script to control multiple blend shapes of skin mesh renderers. The script has floats and an OnValidate() void. Whenever the float changes, the blend shape weight should change. This works normally in real-time, however, when an animator controls the floats of that script, the blend shape weights are no longer changing.
    Code (CSharp):
    1. public float B1Value;
    2. public SkinnedMeshRenderer smr;
    3.  
    4. void OnValidate()
    5. {
    6.      smr.SetBlendShapeWeight(1, B1Value);
    7. }
    What should I do to make OnValidate() work in animations and change blend shapes?
     
    JosephStar318 likes this.
  2. tanmaykulkarni

    tanmaykulkarni

    Joined:
    Nov 5, 2019
    Posts:
    104
    Anyone from Unity Technologies?
     
  3. JosephStar318

    JosephStar318

    Joined:
    Jun 20, 2022
    Posts:
    20
    Unfortunately it's not possible to use OnValidate on runtime because it is editor only function. You can define a float in the animator and access it by using "animator.GetFloat()" in the Update. That way whenever you change the variable in the animator by some animation, it will be updated directly.