Search Unity

Test for existence of a State in Mecanim

Discussion in 'Animation' started by SomeCode, Sep 14, 2014.

  1. SomeCode

    SomeCode

    Joined:
    Jul 5, 2013
    Posts:
    32
    I have a project with a lot of characters. Due to the nature of the game, many of these characters share identical behavior, but not all characters have animations to go with these behaviors, and unfortunately Unity hard crashes when the animator is asked to Play a State that doesn't exist. A simple workaround would be for the animation-playing wrapper method to test for the State before jumping to it with animator.Play, but apparently no such function exists in the Animator class. Oddly enough, surrounding the Play call in a try/catch doesn't work either, as animator.Play seems to be immune to its power, bringing the application to a halt regardless (even when other exceptions within the same block are successfully caught). The only workaround I'm able to come up with for the time being is to make an extra set of booleans that dictate which animations to attempt to play, and which to not, and with the amount of animations and characters in the project, this would be a silly workaround.

    Basically, I either need to be able to test for a State's existence on an Animator component, or prevent the Animator from crashing the application if told to Play a non-existent State.

    Thanks in advance.

    EDIT: "Crashing" is inaccurate - I usually have Error Pause checked which makes Unity pause when an error occurs, and I'm assuming this would be ignored during regular play. Perhaps the explanation is that my own try/catch is being overridden by a hidden try/catch under the hood of Mechanim, but still, it'd be nice to handle this case errorlessly.
     
    Last edited: Sep 14, 2014
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Hi Zidanqiu,

    please log a bug if you can crash unity with Animator.Play.

    There is no way right now to query state existence at runtime, but it on the road map to add more State Machine functionality at runtime .
     
  3. Nodrap

    Nodrap

    Joined:
    Nov 4, 2011
    Posts:
    83
    Did you submit this as a bug as I've hit the exact same problem. Whilst I'm waiting for an animation to come I want to set the code to play it anyway but not crash if its not there. I have pairs of anims that have matching names but one set is incomplete so my "dual call" fails half the time.