Search Unity

How to access to child object from a StateMachineBehaviour

Discussion in 'Animation' started by Nightmarer666, Aug 28, 2022.

  1. Nightmarer666

    Nightmarer666

    Joined:
    Feb 14, 2021
    Posts:
    9
    Hello, I have a state in the animation which needs to make not visible a child object of the Player object in the hierarchy.
    I've been trying to do it by a script in many different ways but I don't know how to get it.
    It says that the object HeadParent, which is the name of the object subchild of the Player, doesn't exist in that context, but it does exists.
    Can anybody help me?

    This is what I have. Can anybody help me, please?

    Code (CSharp):
    1. public class SnorHelmetRemove : StateMachineBehaviour
    2. {
    3.     public Transform headParent;
    4.  
    5.     private void OnEnable() {
    6.         headParent = Transform.Find(HeadParent);
    7.     }
    8.  
    9.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    10.     {
    11.         headParent.gameObject.SetActive(false);
    12.     }
    13. }
     
    Last edited: Aug 29, 2022
  2. Nightmarer666

    Nightmarer666

    Joined:
    Feb 14, 2021
    Posts:
    9
    Why no one answers in this community?