Search Unity

Question Animate a C# script variable by animation?

Discussion in 'Timeline' started by SMal99, Jun 4, 2023.

  1. SMal99

    SMal99

    Joined:
    Mar 23, 2023
    Posts:
    1
    Hello everyone

    I was wondering if it is possible to animate a custom C# field in an animation with the animation tool.

    Context:
    I want to animate a radius around my player while it is doing an animation for a VFX. I want it to be precisely timed in my animation to avoid any synchronization issue.
    I thought that having a public float in my script to control the radius and then adding a property in my animation then changing it with some keyframes could work but apparently it doesn't work like that.
    When I modify the property in my animation file it works as intended but when I select my prefab with the animator and the c# script the value stays the same as it is in the inspector and doesn't change while doing the animation in game.
    When I try changing the values in my prefab keyframes they're automatically assigned to the value showed in the inspector for my script. I guess it's because the animation properties are used to be changed by script but is there a way to do the opposite?