Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question How to detect that variable was changed during animation

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

  1. qweeeeeeee


    Jun 6, 2018
    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;
    5. public class SpriteChanger : MonoBehaviour
    6. {
    7.     public enum SpriteType { FeetFront, FeetSide, WristOpened, WristClosed };
    9.     public SpriteType m_spriteType;
    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.     }
    17.     Sprite SpriteTypeToSprite(SpriteType inType)
    18.     {
    19.         //TODO Find sprite for this object and for given type
    20.         return null;
    21.     }
    22. }