- Home /
Problem is not reproducible or outdated
StateMachineBehaviour get component on awake ??
public class StateCheck : StateMachineBehaviour
{
Animator anim;
private void Awake()
{
// anim = with this animator
}
}
i need to get animator on awake, how can i do that ??
If the animator is attached to another game object you should get it in the start method
void Start()
{
anim = GameObject.Find("YourObjectName").GetComponent<Animator>();
}
If it is attached to the same game object you could get it in awake like so
void Awake()
{
anim = GetComponent()<Animator>();
}
A good article on awake vs start: https://www.monkeykidgc.com/2020/07/Unity%20Lifecycle%20Awake%20Vs%20OnEnable%20Vs%20Start.html
I think smillyfaces is trying to recommend that you make your Animator private and make it serializable so that you can drag the object with animator into the field inside the unity editor.
like this article https://www.monkeykidgc.com/2020/07/Encapsulation.html
Answer by smillyfaces · Jul 29, 2020 at 02:36 PM
if the animator is already attached to the game object ur script is on there is no need to have on awake for this function, another way if the animator is not attached is to make the variable at the top either [serilizable] or public.
what do you mean ??, i want to get component the animator on statemahinebehaviour script,,
one more thing u could do is create a private GameObject called animator then type in awake animator = GameObject.Find("insert object tag"); animator.GetComponent<Animator>();