I’m trying to disable the ‘CharacterMotor’ script on the First Person Controller when I change between cameras in my scene.
I’m using this script -
var cam1 : Camera;
var cam2 : Camera;
function Start()
{
cam1.enabled = true; cam2.enabled = false;
}
function Update()
{
var myScript = GameObject.Find("FirstPersonController").GetComponent("CharacterMotor") ;
if (Input.GetKeyDown(KeyCode.C))
{
cam1.enabled = !cam1.enabled; myScript.enabled = false;
cam2.enabled = !cam2.enabled; myScript.enabled = true;
}
}
And getting this error -
NullReferenceException: Object reference not set to an instance of an object
SwitchCameras.Update () (at Assets/Scripts/SwitchCameras.js:11)
What am I doing wrong? As usual any help is greatly appreciated.
(alternatively if there is a way to switch off/disable the character controller itself temporarily until I switch back to it, that would also be great)
???
Call your cameras Cam1 and Cam2 then in Start() before you enable/disable them do this:
cam1 = GameObject.Find("Cam1");
cam2 = GameObject.Find("Cam2");
My JS is poor though so may need tweaking.
EDIT:
Also shouldn’t you have an if statement for checking the cam1.enabled? like I said not well up on javascript but it looks like you should have:
if(cam1.enabled)
{
// do your code here
}
Sorted ! Just switched the Boolean and getkey parts round and works fine
function Update()
{
var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent("CharacterMotor");
if (cam2Active == false)
{
if(Input.GetKeyDown(KeyCode.C))
{
cam1.enabled = false;
cam2.enabled = true;
freezePlayer.enabled = false;
cam2Active = true;
}
}
else
if (cam2Active == true)
{
if(Input.GetKeyDown(KeyCode.C))
{
cam1.enabled = true;
cam2.enabled = false;
freezePlayer.enabled = true;
cam2Active = false;
}
}
}
Just thought I’d put the answer up in case it might help anybody else in future !