- Home /
Problem with Null Reference Exception??
So i have a script that activates a GUI when you press "e" on it. It's meant to pause the game when you are writing a "secret password" on the GUI. For this, i did the same that I have done with another script, I lock the camera, the movement of the character, the hability to turn the flash light on/off, etc. The problem only one thing is working, when i press "e" it gets displayed, but all the other variables that i want to transform into "false" are not responding... Here is the error:
NullReferenceException: Object reference not set to an instance of an object SafeLockOnE.Update () (at Assets/SafeLockOnE.js:38)
Here is the Script:
var range: float = 5;
private var hit: RaycastHit;
var SafeLockMeca : SafeLock;
var lookAround01 : MouseLook;
var lookAround02 : MouseLook;
var charMotor : CharacterMotor;
var charMotor2 : FPSWalkerEnhaced;
var FlashlightHide : FlashLight;
var HideCursor : HideMouseCursor;
function Start ()
{
SafeLockMeca = gameObject.GetComponent(SafeLock);
lookAround01 = gameObject.GetComponent(MouseLook);
lookAround02 = GameObject.Find("MainCamera").GetComponent(MouseLook);
charMotor = gameObject.GetComponent(CharacterMotor);
charMotor2 = gameObject.GetComponent(FPSWalkerEnhaced);
FlashlightHide = GameObject.Find("FlashLight").GetComponent(FlashLight);
HideCursor = GameObject.Find("Player").GetComponent(HideMouseCursor);
SafeLockMeca.enabled = false;
}
function Update(){
if (Input.GetKeyDown("e")){
var ray = Camera.main.ViewportPointToRay(Vector3(0.5,0.5,0));
if (Physics.Raycast(ray, hit, range)){
SafeLockMeca.enabled = true;
lookAround01.enabled = false;
lookAround02.enabled = false;
charMotor.enabled = false;
charMotor2.enabled = false;
FlashlightHide.enabled = false;
HideCursor.enabled = false;
if (Input.GetKeyDown("KeyCode.Escape")){
SafeLockMeca.enabled = false;
}
}
}
}
Tips: If i delete line 38, and everything that it's related to it, the next line does'nt work either, so only "SafeLockMeca.enabled = true;" it's working, from lookAround01.enabled = false; to HideCursor.enabled = false; is not working...:(
PS:
1) Also i don't know why but i discovered that the GUI with the password gets displayed also if i press "e" near any object with a collider...
2) I would also like if someone can show me a way to close the GUI when i press "ESC"
Yes that's already done, but it still show the same error...:/
Answer by aldonaletto · Oct 26, 2013 at 06:50 AM
Which object is this script attached to? This code could get the right script instances only if all of them (including this script) were attached to the same object - the player, in this case. If this script (and SafeLock) are attached to a different object than the player, Start should be like this:
function Start ()
{
SafeLockMeca = GetComponent(SafeLock); // get SafeLock from this object
var player = GameObject.Find("Player"); // get other scripts from the player
lookAround01 = player.GetComponent(MouseLook);
lookAround02 = Camera.main.GetComponent(MouseLook);
charMotor = player.GetComponent(CharacterMotor);
charMotor2 = player.GetComponent(FPSWalkerEnhaced);
HideCursor = player.GetComponent(HideMouseCursor);
// be careful: if there's more than one flashlight object
// in scene, this line may return the wrong instance:
FlashlightHide = GameObject.Find("FlashLight").GetComponent(FlashLight);
SafeLockMeca.enabled = false;
}
And the Raycast won't do what you expect, whatever it is: it checks for key E whenever any collider is in the middle of the screen and closer enough to the camera. What do you wanna do? Sense E when the mouse is over an existing GUIText or GUITexture? Or when the mouse is over the area where the GUI password should appear?
EDITED: Ok, so you want to enable the GUI code when the player is inside the trigger and E is pressed. You could use OnTriggerStay, but it's more efficient to have a boolean that tells whether the player has entered/exited the trigger. You should also monitor the current GUI state, and enable/disable the other scripts only when the GUI gets disabled/enabled - like this:
private var inTrigger = false; // is player inside trigger?
function OnTriggerEnter(other: Collider){
if (other.CompareTag("Player")) inTrigger = true;
}
function OnTriggerExit(other: Collider){
if (other.CompareTag("Player")) inTrigger = false;
}
private var guiEnabled = false; // current gui state
function Update(){
// if inside trigger and E pressed...
if (inTrigger && Input.GetKey("e")){
SafeLockMeca.enabled = true; // enable gui
}
// ESC disables gui
if (Input.GetKey("escape")){
SafeLockMeca.enabled = false;
}
// if GUI changed its enabled state...
if (SafeLockMeca.enabled != guiEnabled){
guiEnabled = SafeLockMeca.enabled; // update guiEnabled
// disable/enable other scripts accordingly:
lookAround01.enabled = !guiEnabled;
lookAround02.enabled = !guiEnabled;
charMotor.enabled = !guiEnabled;
charMotor2.enabled = !guiEnabled;
FlashlightHide.enabled = !guiEnabled;
HideCursor.enabled = !guiEnabled;
}
}
NOTE: In order to close the GUI when the correct password has been entered, just make SafeLock disable itself - for instance (SafeLock script):
function OnGUI(){
password = GUI.TextField (Rect (10, 10, 200, 20), password, 25);
if (password == "cheat123"){
enabled = false; // disable itself
}
}
The script in the trigger detects any change in SafeLock.enabled property and disables/enables the other scripts as needed.
The GUI pasword script is attached to a cube with a trigger, it will be ok if when the player is on the trigger and presses "e", just then the GUI gets displayed, but i have no clue how to do it, i have been experimenting with something like this, but i don't know if i'm doing it right, or how to implement it:
function OnTriggerEnter (Col : Collider)
{
if(Col.tag == "Player")
{
.....
And if it's possible, to close the GUI on "Esc" key or when you write the password correctly or incorrectly (close it in both cases)
Aldonaletto...,everything worked, you solved all my problems:):). I really apreciate your time and help. I learned a lot with you. Thanks for your patience and your goodwill:)