- Home /
Anyone Else wanna take a wack at the question ? Need help fixing a script error...
Okay
"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); } } if(doorIsOpen){ 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() { audio.PlayOneShot(doorOpenSound); doorIsOpen = true; var myOutpost : GameObject = GameObject.Find("outpost");
myOutpost.animation.Play("dooropen"); }
function shutDoor() { audio.PlayOneShot (doorShutSound);
doorIsOpen = false; var myOutpost : GameObject = GameObject.Find ("outpost");
myOutpost.animation.Play("doorshut");
}
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);
Answer by Jaap Kreijkamp · Feb 21, 2010 at 02:04 AM
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.
well it says the problem is at line : -1 which didn't make any sense to me. The other 2 problems u listed are working as they are suppose to. Also its also saying "expecting }, found '@script'. Just for some reason the batteries are not destroy after the FPC is moving over them...and no sound. :(
Basically everything working except for this part
function OnTriggerEnter (collisionInfo : Collider) {
if(collisionInfo.gameObject.tag == "battery") {
BatteryCollect.charge++;audio.PlayOneShot (batteryCollect);Destroy(collisionInfo.gameObject);}
@script RequireComponent (AudioSource) ;
In your above posted code you're missing one } at the very end. You didn't close the { of your OnTriggerEnter function.
To clarify: Thats's before your @script RequireComponent (AudioSource) ;
Answer by felivans · Nov 20, 2010 at 03:35 AM
on trigger enter doesent suport "collideinfo" it must be (other : Collider) i think
Answer by Loius · Nov 20, 2010 at 03:42 AM
The error very much sounds like one of your GUI calls is used incorrectly.
Answer by schaddemm · Jan 31, 2013 at 10:06 AM
Try commenting:
//Destroy(collisionInfo.gameObject);
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.