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;
cam1.enabled = true; cam2.enabled = false;
var myScript = GameObject.Find("FirstPersonController").GetComponent("CharacterMotor") ;
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.
// do your code here
Sorted ! Just switched the Boolean and getkey parts round and works fine
var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent("CharacterMotor");
if (cam2Active == false)
cam1.enabled = false;
cam2.enabled = true;
freezePlayer.enabled = false;
cam2Active = true;
if (cam2Active == true)
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 !