Search Unity

How to access public variables inside a StateMachineBehaviour?

Discussion in 'Animation' started by Korh1, Sep 11, 2018.

  1. Korh1

    Korh1

    Joined:
    Jul 31, 2018
    Posts:
    35
    Hey guys,

    I'm using the Animation Controller as a Finite State Machine to organize dialogues. Each State in the machine has a public ScriptableObject attached to it containing a bunch of arrays, and I must alter these arrays from within another script.

    "animator.GetCurrentAnimatorClipInfo(0).SetValue(myBoolArray[0] = true)" doesn't work. Apparently, "the call is ambiguous between params int[] and params long[]." Also, there is no AnimationClip running, so that probably wouldn't work anyway.

    So...

    How can I access fields inside a specific State ?

    If that is not possible:

    Is there a way to at least get the [string] name of the previous and/or next State from within the current one's StateMachineBehaviour script?

    That could also help me solve the current problems I'm having. I know you can get their "tagHash," "fullPathHash" and other hashes, but I have no idea what to do with those numbers.
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    you can compare them with pre hashed string

    use the following function to generate a hash from a string
    https://docs.unity3d.com/ScriptReference/Animator.StringToHash.html

    and then you can do something like
    Code (CSharp):
    1.  
    2. if( animator.GetCurrentAnimatorStateInfo(0).fullPathHash == Animator.StringToHash("Base Layer.MyState"))
    3. {
    4. }
    5.  
    it usually a good idea to pre hash all the string that you need on start and then simply use the hash to avoid a managed allocation while evaluating.
     
  3. Korh1

    Korh1

    Joined:
    Jul 31, 2018
    Posts:
    35
    Hey Mecanim-Dev, thanks for answering.

    I'm going to start seeing if I can come up with a solution using StringToHash, but, just to be sure:

    I assume getting a StateMachineBehaviour to access its fields is not, since you didn't mention it. Regarding my second question, ideally what I need is to get a State's name based on the last transition, since that would allow me to take the player's input into account.That can't be done either?
     
  4. Korh1

    Korh1

    Joined:
    Jul 31, 2018
    Posts:
    35
    Oh, nevermind. Without accessing the fields within my StateMachineBehaviour, doing what I must is not possible regardless of being able to single-out the transitioning-to State or not. I'm going to have to come up with a different solution, so... forget it.

    But thanks for clarifying those hashes for me anyway.
     
  5. MxShepherd

    MxShepherd

    Joined:
    Feb 19, 2017
    Posts:
    9