Anyone Else wanna take a wack at the question ? Need help fixing a script error...


"NullRefereceException: Object reference not st to an instance of an object UnityEngine.UIStyle.Draw(Rect position, Boolean isHover, Boolean on, Boolean hasKeyboardFocus)"

appears when I press play. here is the script. An object " the battery" is suppose to be destroyed, make a sound when it does and then a GUI texture of how many batteries were collected is suppose to appear.

This is the script that goes on the FPC -

var batteryCollect : AudioClip;

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 Update() {
    var hit : RaycastHit;
    if(Physics.Raycast (transform.position,transform.forward, hit, 5)) {
        if(hit.collider.gameObject.tag == "outpostDooor" && doorIsOpen == false) {
            currentDoor = hit.collider.gameObject;  
            Door(doorOpenSound, true, "dooropen", currentDoor); 
        doorTimer += Time.deltaTime;    
        if(doorTimer > doorOpenTime){
            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 OpenDoor() {
    doorIsOpen = true;
    var myOutpost : GameObject = GameObject.Find("outpost");    

function shutDoor()  {
    audio.PlayOneShot (doorShutSound);  
    doorIsOpen = false; 
    var myOutpost : GameObject = GameObject.Find ("outpost");       

function Door(aClip : AudioClip, openCheck : boolean, animName : String, thisDoor : GameObject) {
    doorIsOpen = openCheck;

function OnTriggerEnter(collisionInfo : Collider) {
    if(collisionInfo.gameObject.tag == "battery") {

    @script RequireComponent(AudioSource);

It's not easy to give an answer without knowing in which line the exception occurs, the exception should tell you in which file and line number unity was when things got wrong. The null pointer exception means that you're trying to access an object from a variable, but the variable is not set.

In your case this could happen if there isn't an object outpost. That would make:

var myOutpost : GameObject = GameObject.Find("outpost");

result in `myOutpost` being `NULL` (not holding a reference to an object). The line

myOutpost.animation.Play ("doorshut");

would give a null pointer exception as you can't get the `animation` component if `myObject` doesn't point to an object.

look at you last function - missing a }

on trigger enter doesent suport "collideinfo" it must be (other : Collider) i think

The error very much sounds like one of your GUI calls is used incorrectly.

Try commenting:


From what I see this ones your best shot at causing a null reference error if you’ve got everything assigned correctly in the editor.

You could be causing a null reference by destroying some object tagged “battery”(also check that you haven’t tagged something wrong) and destroying some component that lives upon it that some other script wants to access.

Other thing you should do is put parts of your code in comments and see what you need to take away before it starts working again, so that you isolate your problem better.

Other good thing to do is after you hit play you hit pause and go in the editor over the objects and see if theres a missing component somewhere.