Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. 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.