so I tried this and learned I need to edit a variable of a component and MousLook is not a component.
How do I change variables of MouseLook?
Sorry I’m really new to C# and am not sure how to do this.
I don’t know how else to change things. I thought below would work but no.
MouseLook.XSensitivity = 0
When I tried with getcomponent visual studio recognized XSensitivity, YSensitivity, and lockCursor.
but when I do MouseLook. there are no options. Mouse look is public.
I’m so confused.
I just want to edit the fields for mouse look. Any suggestions or help would be so appreciated.
Well it depends where that variable m_MouseLook is defined, if its in the first person controller, then you’d need to do
gameObject.GetComponent<FirstPersonController>().m_MouseLook - but that sounds unlikely, Im guessing m_MouseLook is possibly something locally in your own script…
If MouseLook is a class(/component) its unlikely MouseLook.<something> is a valid option it would need to be static.
So, if MouseLook is a class, the m_MouseLook could be the answer, but you cant just have a box shaped MouseLook called m_MouseLook you would need to populate it, so perhaps m_MouseLook = gameObject.GetComponent<FirstPersonController>().MouseLookl; then you can do m_MouseLook.XSensitivity=2; perhaps. but hey, with info provided too many open questions and not enough info provided
Thank you so much for taking the time to comment and help! I greatly appreciate it I’v been stuck for a couple days now.
FPScontroller is my game object, FirstPersonController is my component and Mouse Look is under that. I’m trying to change FirstPersonController variables from within my inventory script. Basically Im trying to unlock cursor and lock movement when inventory is opened. No matter what I try I cant seem to access these variables. The inventory script is also on the gameobject FPScontroller.
if I could disable and re-enable FirstPersonController that would be the perfect solution
If MouseLook is a property of the FirstPersonController component, then you need a reference to the component first, before you can access its respective properties.
So you would GetComponent<FirstPersonController>(), not for MouseLook (which will be logging a warning about using an invalid type in GetComponent<T>). Then you can access its .MouseLook property (I’m assuming the name here).
Of course you need to be using GetComponent<T> with a reference to the game object that actually has this component. If this is happening on a component on the same game object, then just serialize a reference directly to said component via the inspector.
You do need to describe what particularly isn’t working. Does it throw an error? Does the component’s enabled state not change (confirmed via the inspector)?
stop finding, and getting components, its incredibly unnecerssary, especially as you know (otherwise you’d have had null excerption the first time) you’re on the game object with the controller
Please learn to cache the components and objects, your code, players and the peoples sanity will happier
As Ive just seen what behavior are you expecting when you change enabled to false?
Correct, I’m wanting to disable FPS controller from Inventory script. This inventory script is on the same game object fpscontroller script is on (player)
Is a bad idea. As the null-reference could be happening in two different places here.
You probably want code to inform you of errors so you know more precisely what is going wrong:
var playerGO = GameObject.Find("Player");
if (playerGO != null)
{
if (playerGO.TryGetComponent(out FirstPersonController fpsController) == true)
{
fpsController.enabled = false;
}
else
{
Debug.LogError("Player Game object does not have FirstPersonController component.");
}
}
else
{
Debug.LogError("Cannot find player game object");
}
Nonetheless you don’t want to be using GameObject.Find or similar.
I’ll ask again, can you directly reference this component via the inspector?
Ok, I added this to the top of my script, but now I’m running into a different issue. “FPSController” will not slot into the space via inspector. I ran into this issue before and still have not figured out why my FPSController will not slot into those fields. It feels like a serious handicap.