- Home /
Problem with Null Reference Exeption??:/
Hello:), so this script is meant to deactivate a SafeLock Script at first, and then when the player hits "E", the SafeLock Script gets activated (Shows a keyboard(GUI) where you write the right combination to unlock a safe). The proble is that the activation and deactivation of the SafeLock Script is working (when I press "E" near the safe it shows the GUI). But all the other variables that i wanted affected(walking, moving the camera, run, turn my flashlight on/off), are not being affected.... Unity displays an error that says:
"NullReferenceException: Object reference not set to an instance of an object SafeLockOnE.Update () (at Assets/SafeLockOnE.js:37)"
So when the player aproaches the safe and press "E", the Gui gets displayed, but i can still walk, move the camera, run, turn my flashlight on/off, etc...What should I do?? Please Help
This is the Scipt:
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;
}
}
}
Concerning you Null reference exception, where is it happening? Is it on FlashLightHide?
From line 37 to line 42 are not working, i can tell because line 35 works and displays the GUI of the Safe(keyboard), but the other things (lookaround, char motor, etc) are not working (because I can still move the camera, walk, etc). I tested deleting for example line 37, 16 and 5(everything related to lookAround01), and the same error appears, only that this time reffering to the next thing, and the next thing, etc... The only thing that works is the "SafeLock$$anonymous$$eca" thing...Any Ideas??
Answer by jacksmash2012 · Oct 25, 2013 at 07:26 PM
This line here in your Start function:
FlashlightHide = GameObject.Find("FlashLight").GetComponent(FlashLight);
Make sure you throw in a debug statement right after the fact that ensure you grabbed the reference and that FlashlightHide is not null at this point.
Actually from line 37 to 42 doesn't work, and that's really weird because in an almost identical script, everything works for me, but in this one, it doesn't. The similar, but working script:
var lookAround01 : $$anonymous$$ouseLook;
var lookAround02 : $$anonymous$$ouseLook;
var char$$anonymous$$otor : Character$$anonymous$$otor;
var char$$anonymous$$otor2 : FPSWalkerEnhaced;
var FlashlightHide : FlashLight;
var HideCursor : Hide$$anonymous$$ouseCursor;
var respawnTransform : Transform;
static var playerIsDead = false;
function Start ()
{
lookAround01 = gameObject.GetComponent($$anonymous$$ouseLook);
lookAround02 = GameObject.Find("$$anonymous$$ainCamera").GetComponent($$anonymous$$ouseLook);
char$$anonymous$$otor = gameObject.GetComponent(Character$$anonymous$$otor);
char$$anonymous$$otor2 = gameObject.GetComponent(FPSWalkerEnhaced);
FlashlightHide = GameObject.Find("FlashLight").GetComponent(FlashLight);
HideCursor = GameObject.Find("Player").GetComponent(Hide$$anonymous$$ouseCursor);
}
function Update ()
{
if (playerIsDead == true)
{
lookAround01.enabled = false;
lookAround02.enabled = false;
char$$anonymous$$otor.enabled = false;
char$$anonymous$$otor2.enabled = false;
FlashlightHide.enabled = false;
HideCursor.enabled = false;
}
}
function OnGUI ()
{
if (playerIsDead == true)
{
if (GUI.Button(Rect(Screen.width*0.5-50, 200-20, 100, 40), "Respawn"))
{
RespawnPlayer();
}
if (GUI.Button(Rect(Screen.width*0.5-50, 240, 100, 40), "$$anonymous$$enu"))
{
Debug.Log("Return to $$anonymous$$enu");
}
}
}
function RespawnPlayer ()
{
transform.position = respawnTransform.position;
transform.rotation = respawnTransform.rotation;
gameObject.Send$$anonymous$$essage("RespawnStats");
lookAround01.enabled = true;
lookAround02.enabled = true;
char$$anonymous$$otor.enabled = true;
char$$anonymous$$otor2.enabled = true;
FlashlightHide.enabled = true;
HideCursor.enabled = true;
playerIsDead = false;
Debug.Log("Player has respawned");
}
@script RequireComponent(CharacterController)
Your answer
Follow this Question
Related Questions
How to draw GUI Text from code 1 Answer
Object reference exists but also doesn't? 1 Answer
NullReferenceException in for-loop 0 Answers
How to disable my Script and Gui 1 Answer