- Home /
How do I disable Script on FP Controller? (Solved)
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)
???
Answer by Digital-Phantom · Jan 11, 2015 at 05:24 AM
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 !
Answer by Mmmpies · Jan 10, 2015 at 05:08 PM
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
}
The camera switching works fine, its the actual FirstPersonController component I want to access and enable/disable
Ah sorry, working in java never sits well with me. How about adding a Debug.Log to show if myScript has been picked up?
Debug.Log("myScript = " + myScript);
just after you set it.
EDIT:
And I'm being an idiot again, looks like you're searching for a FirstPersonController and not the Player or enemy.
Search for the name of the character that the character motor and and first person controller is on then GetComponent("Character$$anonymous$$otor")
EDIT2:
Really should make that clearer, both FirstPersonController and Character$$anonymous$$otor are components that generally go on a Player GameObject.
So if you have a Player GameObject that these are on then you reference them with:
var myScript = GameObject.Find("Player").GetComponent("Character$$anonymous$$otor");
//or if using Tags
var myScript = GameObject.FindGameObjectWithTag("Player").GetComponent("Character$$anonymous$$otor");
Unless you've called your Player FirstPersonController (which is a bad idea).
O$$anonymous$$ I've decided to re-write the script. Still doing what I want but hopefully making it easier to follow (and add to later)
var cam1 : Camera;
var cam2 : Camera;
var cam2Active : boolean = false;
function Start()
{
cam1.enabled = true;
cam2.enabled = false;
}
function Update()
{
var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent("Character$$anonymous$$otor");
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
{
if(cam2Active == false)
{
cam1.enabled = false;
cam2.enabled = true;
freezePlayer.enabled = false;
cam2Active = true;
}
}
else
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
{
if(cam2Active == true)
{
cam1.enabled = true;
cam2.enabled = false;
freezePlayer.enabled = true;
cam2Active = false;
}
}
}
$$anonymous$$y problem now is that the main camera won't re-enable? I'm fairly sure its to do with the 'cam2Active' boolean not being recognised by the second IF statement. I know I need to add it to the IF statement, just not sure where (tried a couple of places with no luck)
any suggestions ???