- Home /
NullReferenceException: Object reference not set to an instance of an object
I'm sure that this has been answered somewhere else but I haven't found it yet and I sort of need to turn in this project for class tonight.
I am using Will Goldstone's "Unity Game Development Essentials" for a class and I am having a weird error creep up. (See title)
Below is my code:
private var doorIsOpen : boolean = false;
private var doorTimer : float = 0.0;
private var currentDoor : GameObject;
var batteryCollect : AudioClip;
var doorOpenTime : float = 3.0;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
function Update () {
var hit : RaycastHit;
if(Physics.Raycast (transform.position, transform.forward, hit, 5)) {
if(hit.collider.gameObject.tag=="outpostDoor" && doorIsOpen == false && BatteryCollect.charge >= 4){
GameObject.Find("Battery GUI").GetComponent(GUITexture).enabled=false;
currentDoor = hit.collider.gameObject;
Door(doorOpenSound, true, "dooropen", currentDoor);
}
}
else if(hit.collider.gameObject.tag=="outpostDoor" && doorIsOpen == false && BatteryCollect.charge < 4){
GameObject.Find("Battery GUI").GetComponent(GUITexture).enabled=true;
TextHints.message = "The door seems to need more power...";
TextHints.textOn = true;
}
if(doorIsOpen){
doorTimer += Time.deltaTime;
if(doorTimer > 3){
Door(doorShutSound, false, "doorshut", currentDoor);
doorTimer = 0.0;
}
}
}
/*
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.gameObject.tag == "outpostDoor" && doorIsOpen == false){
currentDoor = hit.gameObject;
Door(doorOpenSound, true, "dooropen", currentDoor);
}
}
*/
function Door(aClip : AudioClip, openCheck : boolean, animName : String, thisDoor : GameObject){
audio.PlayOneShot(aClip);
doorIsOpen = openCheck;
thisDoor.transform.parent.animation.Play(animName);
}
function OnTriggerEnter(collisionInfo : Collider){
if(collisionInfo.gameObject.tag == "battery"){
BatteryCollect.charge++;
audio.PlayOneShot(batteryCollect);
Destroy(collisionInfo.gameObject);
}
}
@script RequireComponent(AudioSource)
For some reason I am getting this error:
NullReferenceException: Object reference not set to an instance of an object PlayerCollisions.Update () (at Assets/Scripts/PlayerCollisions.js:24)
But I am very confused because line 24, which is:
else if(hit.collider.gameObject.tag=="outpostDoor" && doorIsOpen == false && BatteryCollect.charge < 4){
Will not work at all if "else" is there, but once I remove it, the error continues BUT it works like its suppose to.
I'm very confused, so if anyone would be able to look at this, I would greatly appreciate it.
Thanks!
I see batteryCollect and BatteryCollect, maybe this has something to do with it? On a side note: I always prefer to nest if-statements 'as much as' possible. $$anonymous$$eaning, putting the first two checks (which are the same in both cases) in one if-statement and then check for the charge property. You can then be sure that the first two are true when you check for the third.
I changed the batteryCollect and BatteryCollect and it didn't change anything.
As far as the code goes, I'm suppose to copy it straight from the book and as far as I can tell, this is what the book wants...I think
Answer by DaveA · Oct 10, 2011 at 09:52 PM
I hate it when code isn't tested before it's published, unless it has such a disclaimer on it.
It should be 'batteryCollect' with a small b, because that's how the variable is declared above. That, or the hit doesn't have a collider, or the collider doesn't have a game object.
Why not run this in the debugger, where it will stop on this line, and you can inspect the variables and see what isn't existing? If you don't want to mess with the debugger, you can use Debug.Log to 'dump' the variables (before this problem line), to see what values. One of them will be null.
But it's probably that 'b' thing.
I did change everything to say 'batteryCollect' rather than the B.
$$anonymous$$y debugger doesn't come up for some reason... It's grayed out.
Your answer
