0
\$\begingroup\$

I'm aware of how SetAnimatorLayerWeight works, I'm looking for a way to iterate through all layers of my controller at runtime.

I've four layers and expect more, and need to update them at once, in essence, what I'm trying to do is something like this.

To clarify: The code below is boilerplate of what I'm trying to do.

    void SetCurrentWeaponLayerWeight(int index)
    {
        _animator.SetLayerWeight(index, 1);

        foreach(AnimatorLayer l in Animator.Layers)
        {
            if (l.index != index)
                _animator.SetLayerWeight(l.index, 0);
        }
    }
\$\endgroup\$
1
\$\begingroup\$

The problem lies in your use of Animator instead of _animator in your iterator. (I've used a standard for loop for code clarity)

void SetCurrentWeaponLayerWeight(int index)
    {
        _animator.SetLayerWeight(index, 1);

        for(int i=0;i< _animator.layerCount;i++)
        {
            if (i != index)
                _animator.SetLayerWeight(i, 0);
        }
    }
\$\endgroup\$
  • 1
    \$\begingroup\$ I don't see why this code should do anything different than the one in the question. You really should put an explanation, a code only answer is also usually not well received. \$\endgroup\$ – John Hamilton Nov 21 '17 at 5:13
  • \$\begingroup\$ Updated. Good catch \$\endgroup\$ – Stephan Nov 21 '17 at 14:12
  • \$\begingroup\$ This is the answer, I had just completely missed that layerCount existed, silly me. \$\endgroup\$ – ag4w Nov 30 '17 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.