I am trying to go through a tutorial on Unity (Will Goldstone’s book 3.x Game Development Essentials) and keep running into an error messages and I just can’t figure out what is wrong with my code. It’s from Chapter 5, I am writing this in Javascript. For what i can see I have everything correct–the null reference comes up in my OpenDoor function line 2 & in my shutDoor function line2.:
Null Reference Exception
PlayerCollisionsjs.OpenDoor(UnityEngine.GameObject door) (at Assests/Scripts
/PlayerCollisionsjs.js: 47 **and 37 the lines refecenced below).
What am I missing? My OpenDoor & CloseDoor sounds are loaded and the code works if I remove the shut Door requests…any thought
#pragma strict
private var doorIsOpen : boolean = false;
private var doorTimer : float = 0.0;
private var currentDoor : GameObject;
var doorOpenTime : float = 3.0;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
function Start () {
}
function Update () {
if(doorIsOpen) { doorTimer += Time.deltaTime;
if(doorTimer > doorOpenTime) {
shutDoor(currentDoor);
doorTimer = 0.0f;
}
}
}
function OnControllerColliderHit(hit : ControllerColliderHit){
if(hit.gameObject.tag == "playerDoor" && doorIsOpen == false) {
currentDoor == hit.gameObject;
OpenDoor(hit.gameObject);
OpenDoor(currentDoor);
}
}
function OpenDoor(door: GameObject) {
doorIsOpen = true;
door.audio.PlayOneShot(doorOpenSound);
door.transform.parent.animation.Play("dooropen");
}
function shutDoor(door : GameObject) {
doorIsOpen = false;
door.audio.PlayOneShot(doorShutSound);
door.transform.parent.animation.Play("doorshut");
}
I did see that someone else got stuck on the same code, but we don’t have the same problem, my audio works before I insert the shutDoor function.
I think you did not assign open and shut door sounds to above two audio clips, 1st assign these sounds to your variables then your error will be finished.