# Iterating through all animator layers in Unity Mecanim

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);
}
}


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);
}
}

• 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. – John Hamilton Nov 21 '17 at 5:13
• Updated. Good catch – Stephan Nov 21 '17 at 14:12
• This is the answer, I had just completely missed that layerCount existed, silly me. – ag4w Nov 30 '17 at 1:08