Enable/Disable script from inside another script
So I'm trying to disable the FirstPersonController script, from the Standard Assets, and really disabling it isn't the problem.
The idea is that the player holds the "E" key to do something, whilst the "E" is held though the player controller should be disabled so the player can't move or look around. That part works fine, my code disables the FirstPersonController just fine. But when I let go of "E" I get thrown an error.
The error reads "NullReferenceException: Object reference not set to an instance of an Object"
My code is as follows public class LightSwitch : MonoBehaviour { public FirstPersonController fpsController;
void Start()
{
}
void Update()
{
if (collide && Input.GetKey("e"))
{
charge += Time.deltaTime;
fpsController.enabled = false;
}
if (Input.GetKeyUp("e"))
{
charge = 0f;
fpsController.enabled = true;
}
}
It looks to me as if the error is saying I haven't referenced the script I'm enabling, when I clearly have if I have literally JUST disabled it in the same code.
Your answer
Follow this Question
Related Questions
A Problem With A Script Of GUI.HorizontalSlider It Do not Move? 0 Answers
I have a problem with this script and an error appeared, does anyone know why? 0 Answers
How to reference this game object to another class 0 Answers
Can someone tell me what is wrong with this script? 1 Answer
Im having a problem with my C# code 1 Answer