i want to change the Layer in the Animator with Script. It works only on one Object, not on the Prefab.
I think it is because the else statement is setting the layer always to 0 because it is in Update. So it works only on the first Object. If i disable the first Object, the other works. So only one at a time is working.
How can i write it different, but with the same outcome?
Yes, if PlayerTrigger is ever false and this is in update, then it would go back to 0.
You need to only set it on the event that makes PlayerTrigger true… I don’t know what causes that, so I can’t really be more specific to your use case.
Well technically that shouldn’t set it back to 0 immediately in the else because you don’t set PlayerTrigger to false until exit.
But still, that can be shortened up extensively:
using UnityEngine;
public class LayerChange : MonoBehaviour {
public Animator animator;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
animator.SetLayerWeight (3, 1f);
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
animator.SetLayerWeight (3, 0f);
}
}
}
There’s no need to be calling Update constantly. That’s less performant to do that.
Now… what are you expecting to happen when you set the layer weight to 0? You say “change layers”, but setting the layer weight to 0 doesn’t “change” the layer, it just drops the impact of that layer on the overall animation.