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!