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

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:
    11
    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:
    2,555
    Replace AnimationState with var so the compiler picks the type for you.