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!

  1. I’m sure that your Component isn’t called “Mouse Look” with a space :slight_smile: 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?

  2. To avoid these kind of errors, use the UnityEngine.Assertion namespace. It’s really easy and will help you to find these errors early.
    Unity 5.1 Assertion Library – The Knights of Unity