error ... not a UnityEngine.Component

var gun : Transform;
var nextPos = -0.2754517;
var nextField = 40.0;
var nextPos2 = -0.4447794;
var dampVelocity = 0.4;
var dampVelocity2 = 0.4;
var dampVelocity3 = 0.4;

 function Update () {
    var newPos = Mathf.SmoothDamp(gun.transform.localPosition.x, nextPos, dampVelocity, .3);
    var newField = Mathf.SmoothDamp(Camera.main.fieldOfView, nextField, dampVelocity2, .3);
    var newPos2 = Mathf.SmoothDamp(gun.transform.localPosition.y, nextPos2, dampVelocity3, .3);
    
    gun.transform.localPosition.x = newPos;
    gun.transform.localPosition.y = newPos2;
    Camera.main.fieldOfView = newField;
    
    if (Input.GetButton("Fire2")) {
        //adjust viewpoint and gun position
        nextField = 40.0;
        nextPos = -0.7604523;
        nextPos2 = -0.4838691;
        
        //slow down turning and movement speed
        GetComponent("MouseLook").sensitivityX = 2;
        camera.main.GetComponent("MouseLook").sensitivityX= 2;
        camera.main.GetComponent("MouseLook").sensitivityY = 2;
    } else {
        //adjust viewpoint and gun position
        nextField = 60.0;
        nextPos = -0.2754517;
        nextPos2 = -0.4447794;
        
        //speed up turning and movement speed
        GetComponent("MouseLook").sensitivityX = 6;
        camera.main.GetComponent("MouseLook").sensitivityX = 6;
        camera.main.GetComponent("MouseLook").sensitivityY = 6;
    }
 }

Any help is really appreciated.

1 Answer

1

Your problem is with your GetComponent() calls. When you call it with a string like this, Unity does not know the type of the return value. You can cast it, or a better method is to just call it with the type (i.e. no quotes):

GetComponent(MouseLook).sensitivityX = 2;

You are calling GetComponent a bunch of times for the same component. It would be better to call it once and save the return value:

var ml : MouseLook = GetComponent(MouseLook);
ml.sensitivityX = 2;
ml.sensitivityY = 2;