GameObject.Find GetComponent C#

Just wondering why this isnt working:

 GameObject.Find(“first_person_controller”).GetComponent<MouseLook>().enabled = false;

everything is named correctly. I did a debug and it prints NULL so I’m not really sure what to do from here

Error message:

NullReferenceException: Object reference not set to an instance of an object

try to make sure that the Game Object ( “first_person_controller” ) are active when the game trying to execute that code. this is because GameObject.Find function only returns active Game Objects.

First make sure first_person_controller name is same as seen in hierarchy.And then check in an if statement whether the specified component is attached to it.

if( GameObject.Find(“first_person_controller”).GetComponent()!=null)

You Cant Just Use “Find”. The Best Way Would Be To Use set a tag on the Object you want to find and use “FindWithTag” instead of “Find”. Works Every time.

I see what the problem could potentially be.

In one of the screenshots you posted, I see there’s an arrow pointing down on the “first_person_controller” object, indicating it has children.

Make sure the MouseLook script is attached to the object first_person_controller and NOT its children, or else you will get a null with you current code.