How do I access the animator in a child using C#

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#?

void start()

		anim = GetComponentInChildren<Animator> ();
		//broken check for accessing animator of child

	void FixedUpdate()
		//grounded = Physics.OverCircle(groundCheck.position,groundRadius,whatIsGround);
//		anim.setBool("ground",grounded);

		float move= Input.GetAxis ("Horizontal"); 

		rigidbody.velocity = new Vector3(move * maxSpeed,rigidbody.velocity.y); 

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.

That is the correct syntax. So if it fails you do not have an active component on a child.

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:

Hope it helps!