- Home /
Unity 5 Mouse Look Problem
So what I try to do is to make a script which will check whether the mouse is over an object, then it will check if it is a rigidbody and if the user presses "R" and if so it will disable the mouse look sensitivity x and y and will rotate the object with mouse movement.
The problem is that Unity keeps giving me these error: 'XSensitivity' is not a member of 'UnityEngine.Component' 'YSensitivity' is not a member of 'UnityEngine.Component'
Here's the script:
#pragma strict
var speed : float = 1.0;
var player : GameObject;
function Start () {
}
function Update () {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit)) {
if(hit.collider.gameObject.GetComponent("Rigidbody") == null) {
if(Input.GetKeyDown(KeyCode.R)) {
player.GetComponent("Mouse Look").XSensitivity = 0;
player.GetComponent("Mouse Look").YSensitivity = 0;
hit.collider.gameObject.transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed);
} else {
player.GetComponent("Mouse Look").XSensitivity = 5.5;
player.GetComponent("Mouse Look").XSensitivity = 5.5;
}
}
}
}
What's the problem? Thanks!
Answer by Jordi-Bonastre · Apr 28, 2016 at 04:19 PM
I'm sure that your Component isn't called "Mouse Look" with a space :) Maybe the string is "MouseLook". In any case, why don't you use the call to GetComponent with the Type instead to use it with the string?
To avoid these kind of errors, use the UnityEngine.Assertion namespace. It's really easy and will help you to find these errors early. http://blog.theknightsofunity.com/unity-5-1-assertion-library/
Thanks! I'll check it out! Also, I have tried using $$anonymous$$ouseLook without a space but nothing changed. Also, I tried using this ins$$anonymous$$d:
player.GetComponent.<$$anonymous$$ouseLook>().XSensitivity = 0;
Any ideas?