Change the smooth

My attempt at changing the smooth time of First Person Controller from script has resulted in this error:

ArgumentException: GetComponent requires that the requested component ‘MouseLook’ derives from MonoBehaviour or Component or is an interface.

I found a couple of threads relating to this same issue, describing a somewhat complicated workaround to resolve it. So it’s a bit confusing

Could there be a straightforward fix to this one?

Did you try…

GetComponent<FirstPersonController>().mouseLook.smoothTime = myValue;
1 Like

As @Malleck666 Mentioned, you need to use GetComponent on the actual script, you’re trying to use it on a property of a component instead of the component.

1 Like

Thank you for your help guys…

I tried the solutions however I do receive an error:

I’ve been trying a few other solutions as well, but I can’t find a good one. Any thoughts?

According to the images, you have a typo… “mouseLook” should be “MouseLook” i think.

1 Like

Unity inspector auto-capitalizes the first letter in pascal case, so you can’t go by inspector, especially when a custom inspector is used and thus completely custom names can be displayed instead.

You need to make better use of the auto-complete feature in your IDE (which I assume is Visual Studio). When you type that period before “mouseLook”, you would have gotten a list of exposed members that you can access, and typing “mouse” should have narrow the list down to only members with the word “mouse” in them, and thus would have given you the correct variable name to access. In this cause, that script has Mouse Look defined as “m_MouseLook”.

1 Like

Thank you for your help guys,

I have tried a number of solutions now including the ones you have all provided.

The first and second line throw the text editor errors visible,
The third and fourth line throw the same errors at runtime, mentioned in my original post.

I should also mention that I am trying to access the FirstPersonController script a component of FPSController, from a script on the FirstPersonCharacter GameObject. So I am calling the parent from the child:

3204601--245085--upload_2017-8-31_22-35-53.png

Thank you for your advice, Invertex about the IDE. Also a good choice of username :slight_smile:

MouseLook does not derive from Monobehaviour or Component. It is just a class. So you cannot use “GetComponent” on it.
Also, if you opened up the FirstPersonController script, you would see that the MouseLook property is “private”, so it cannot be accessed from outside. So your choices are:

  1. Create a “MyFirstPersonController” script or something like that, and have it derive from “FirstPersonController”. That way you will have access to all the private members. You’ll assign that script as your controller instead.

  2. Use Reflection to get a reference to the private variables you want. This is slightly more complex but it works too.

  3. Just edit the FirstPersonController script, it’s just a .CS file. You can change the MouseLook m_MouseLooks variable to “public” so you can access it.

1 Like

It works perfect now… I changed the MouseLook variable to public as you said! Thank you very much for your help…

1 Like