How to reference variables of the script type from another object?

Sorry about the strangely worded question. I’m trying to disable the Character Motor script that comes with the first person controller, and I’m running into problems. When I disabled the MouseLook script, which is on the same object as my script, I used

MouseLook mouseLook;

and then assigned that variable the script component in the Start() function. However, when I attempt the same thing

CharacterMotor motor;

with the Character Motor script, which is on a different object, I get an error telling me the type or namespace “CharacterMotor” cannot be found. How do I fix this?

So, from one script on an object you are trying to disable the CharacterMotor script of another gameobject? This is done by first acquiring a reference to the GameObject that has the CharacterMotor script attached. Then, you want to GetComponent() from that GameObject

Firstly, you want to get a reference to the GameObject. Maybe you are already using a Raycast to find the CharacterMotor-object? In this case you would access collider.gameobject of the RaycastHit generated by a successful Raycast. Perhaps you already know the name of the object. For that you could use GameObject.Find(“CharacterMotorObject_Name”);. Otherwise, its possible to add a public member to your script where you can provide the GameObject from the editor.

Secondly, given the GameObject, you would use GetComponent, as fafase suggested, like this:

CharacterMotor motor = characterMotorObject.GetComponent<CharacterMotor>();

You would want to do this wherever you can acquire that gameobject at runtime, so unless you are including said gameobject to your script from the editor, you will not be able to do it from Start().