How can I disable controls?

I know this question has been asked, but I can’t get it to fire right. I’ve read a few solutions, but can’t seem to work it out. I’m trying to cut controls for cinematic scenes with the first person controller.

Right now I have a wonky set up I think…

I have a script on both the camera and the player controller.

Camera Script

#pragma strict

function EnableControls(){

	var CamControls = GetComponent.<MouseLook>();
	CamControls.enabled = CamControls.enabled;

	var PlayerControls: GameObject;
	
	PlayerControls.GetComponent(DisablePlayerControls).EnablePlayerControls();

}

function Start () {

	var CamControls = GetComponent.<MouseLook>();
	CamControls.enabled = !CamControls.enabled;

}

function Update () {

}

The script on the Player controll

#pragma strict

function EnablePlayerControls(){

var MouseLook = GetComponent.<MouseLook>();
var Motor = GetComponent.<CharacterMotor>();
var Controller = GetComponent.<FPSInputController>();

MouseLook.enabled = MouseLook.enabled;
Motor.enabled = Motor.enabled;
Controller.enabled = Controller.enabled;

}

function Start () {

var MouseLook = GetComponent.<MouseLook>();
var Motor = GetComponent.<CharacterMotor>();
var Controller = GetComponent.<FPSInputController>();

MouseLook.enabled = !MouseLook.enabled;
Motor.enabled = !Motor.enabled;
Controller.enabled = ! Controller.enabled;
}

function Update () {

}

I created an animation on the main camera using the built in animation feature. I try to fire an event from the animation which is a function from an instance of the script on the player control.

Errors I get are…

  1. NullReferenceException: Object reference not set to an instance of an object
    DisableCameraControls.EnableControls () (at Assets/Scripts/MenuScripts/DisableCameraControls.js:12)

  2. AnimationEvent ‘EnableCameraControls’ has no receiver!

Thanks in advanced to anyone who can help me sort this out. Thanks guy :slight_smile:

In order to disable controls in the First Person Controller prefab, a better alternative is to get all scripts of interest at Start and just enable/disable them when needed - like this:

#pragma strict

private var MLook1: MouseLook;
private var MLook2: MouseLook;
private var ChMotor: CharacterMotor;

function Start(){
  MLook1 = GetComponent(MouseLook);
  MLook2 = Camera.main.GetComponent(MouseLook);
  ChMotor = GetComponent(CharacterMotor);
}

function EnablePlayerControls(enable: boolean){
  MLook1.enabled = enable;
  MLook2.enabled = enable;
  ChMotor.enabled = enable;
}

Notice that you don’t need a camera specific script, since this one does the job - just call EnablePlayerControls(true) to enable the controls, or pass false to disable them. You don’t need either to disable FPSInputController - this script just passes info to CharacterMotor. Actually, you may have two different behaviours: disabling FPSInputController disables control but the character finishes its current movement - if it’s falling, for instance, it will stop only when reaching ground. Disabling CharacterMotor, on the other hand, will stop the character immediately - it may get frozen in the middle of a fall, in the example above.

NOTE:Don’t use variables with the same name of scripts or other components - like MouseLook, for instance. The variable hides the original class (script or component) and you get lots of errors or warnings.