Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to detect that variable was changed during animation

Discussion in 'Animation' started by qweeeeeeee, Jun 12, 2022.

  1. qweeeeeeee

    qweeeeeeee

    Joined:
    Jun 6, 2018
    Posts:
    1
    There is a public variable in my script. When this variable changes I would like to detect this change and run some functions.

    Usually I would use C# properties for this. But as I noticed C# properties do not appear in the editor and therefore it seems that they cannot be animated. And it is important to animate this variable.

    So I added public variable m_spriteType and animated it. Now I wonder how to detect that this variable was changed during animation. Is there any better way than comparing old and new value in Update()?

    I checked messages in MonoBehaviour but didn't find anything that looks as message that notifies that value was changed during animation.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpriteChanger : MonoBehaviour
    6. {
    7.     public enum SpriteType { FeetFront, FeetSide, WristOpened, WristClosed };
    8.  
    9.     public SpriteType m_spriteType;
    10.  
    11.     //I Would like to call this function each time m_spriteType changes
    12.     void OnSpriteTypeChanged()
    13.     {
    14.         //TODO Get sprite renderer and set sprite from SpriteTypeToSprite(m_spriteType) to it
    15.     }
    16.  
    17.     Sprite SpriteTypeToSprite(SpriteType inType)
    18.     {
    19.         //TODO Find sprite for this object and for given type
    20.         return null;
    21.     }
    22. }
    23.