I have a playerobject named player. Player has a child mesh with animation and an animator attached. The control script is on the Player. I keep getting null reference exception error on my setbool
What is the correct declaration for getting the animator component of a child in C#?
thanks!
It doesn’t look like anything is wrong with your code. Note that if the Animator script, or the Jack_gameface GameObject is disabled at the time GetComponentInChildren is being called, it will not find the Animator component. If either of those things are starting out disabled, that could be your problem.
Also, GetComponent and similar search functions are slow. When possible, it’s safer and more performant to make a public field and drag a reference to it in the inspector. This works around your issue entirely, which may not be desirable at the moment if you’re trying to learn.
Another suggestion would be to use the debugger or Debug.Log to ensure it’s the ‘anim’ field that’s null, just to ensure you’re looking in the right place.
If anyone is interested, and if the chosen answer didn’t serve you because you wanted something more code-based, I’ve created a script for the generic version of this: