Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

InvalidCastException when trying to get all AnimationStates in Animation

Discussion in 'Animation' started by dvillaverde-drakhar, Jul 12, 2021.

  1. dvillaverde-drakhar

    dvillaverde-drakhar

    Joined:
    Aug 29, 2018
    Posts:
    9
    Hi,I'm trying to get all AnimationStates in one Animation component. This component is contained inside a Prefab and is loaded using AssetGraph.

    When I try to get all AnimationStates using:

    Code (CSharp):
    1. foreach (AnimationState state in (loadedAsset as Animation))
    An "InvalidCastException: Specified cast is not valid." is thrown.

    What I am doing wrong?

    This way of getting AnimationStates is shown in the Scripting API page of the Animation component and if I try to debug it iterating with object type instead of AnimationState, Visual Studio shows the objects as AnimationStates...

    Thank you
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    1,773
    Replace AnimationState with var so the compiler picks the type for you.
     
unityunity