[SIMPLE] Layer Weight Change Animator

Hey,

i want to Set the Layer Weight and it is working on my Test Cube
But if i duplicate the Cube, then it is only working on one of them.
If i untick one Cube the other works again. But never both. Dont know why. Any Ideas?

Thank you very much for your Time

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestLayerChange : MonoBehaviour
{
   public Animator animator;                    // Assign Animator
   public bool PlayerTrigger = false;       // Set Bool if Player enter the Trigger

    void Update ()
    {
        if (PlayerTrigger)
        {
            animator.SetLayerWeight (3, 1f);   // If Player enter Trigger then Set the Layer Weight to 1
        }
        else
        {
            animator.SetLayerWeight (3, 0f);   // If Player leave Trigger then Set the Layer Weight to 1
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            PlayerTrigger = true;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.tag == "Player")
        {
            PlayerTrigger = false;
        }
    }
}

well, what is ‘otherObject’ ? Are you possibly setting the value on the same animator (twice)?

Thanks for the response. I did not need the Start Function so i deleted that line.
But i have the same Problem. What do you mean with setting it twice on the same animator? Maybe thats why, but how else can i do it?

Maybe the picture get a better understanding of my problem

Okay got it. No need of Update either. Thanks

Sorry, I’m actually not sure. Though, I think you could set the weight in the enter/exit instead of polling for it in Update, if you do figure out why it’s not working. :slight_smile:

lol. You responded as I was still wondering. Post your solution or write what was wrong/how you fixed it? :slight_smile:

I just put it in the OnTriggerEnter/Exit function and it now works for all Prefabs :smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestLayerChange : MonoBehaviour {


    public Animator animator;                       // Assign Animator
    public bool PlayerTrigger = false;       // Set Bool if Player enter the Trigger



    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            PlayerTrigger = true;
            animator.SetLayerWeight (3, 1f);
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.tag == "Player")
        {
            PlayerTrigger = false;
            animator.SetLayerWeight (3, 0f);
        }
    }
}

Interesting. Maybe the variable was not set properly when you copied the other one.
Anyways, it’s a good fix as moving it to that portion of the code is more sensible anyways :wink: