i have redone the code serveral times and i dont know where i am going wrong when i walk to the fire after collecting the matches i get the error - NullReferenceException: Object reference not set to an instance of an object PlayerCollisions.lightFire () (at Assets/Scripts/PlayerCollisions.js:97) PlayerCollisions.OnControllerColliderHit (UnityEngine.ControllerColliderHit hit) (at Assets/Scripts/PlayerCollisions.js:42) UnityEngine.CharacterController:Move(Vector3) CharacterMotor:UpdateFunction() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js:229) CharacterMotor:FixedUpdate() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js:331)
My Code is :
private var haveMatches : boolean = false;
private var doorIsOpen : boolean = false;
private var doorTimer : float = 0.0;
private var currentDoor : GameObject;
private var fireOn : boolean = false;
var matchGUI : GameObject;
var doorOpenTime : float = 3.0;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
var batteryCollect : 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){
currentDoor = hit.collider.gameObject;
Door(doorOpenSound, true, "dooropen", currentDoor);
GameObject.Find("Battery GUI").GetComponent(GUITexture).enabled=false;
}
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.collider.gameObject == GameObject.Find("campfire")){
if(haveMatches){
haveMatches = false;
lightFire();
}else if(!fireOn){
TextHints.textOn=true;
TextHints.message = "I need to find of way of lighting this...";
}
}
var crosshairObj : GameObject = GameObject.Find("Crosshair");
var crosshair : GUITexture = crosshairObj.GetComponent(GUITexture);
if(hit.collider == GameObject.Find("mat").collider){
CoconutThrow.canThrow = true;
crosshair.enabled = true;
TextHints.textOn = true;
TextHints.message = "Hit 'em, pardner, and get yur battery on!";
GameObject.Find("TextHint GUI").transform.position.y = 0.2;
}
else{
CoconutThrow.canThrow = false;
crosshair.enabled = false;
GameObject.Find("TextHint GUI").transform.position.y = 0.5;
}
}
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);
}
if(collisionInfo.gameObject.name == "matchbox") {
Destroy(collisionInfo.gameObject);
haveMatches = true;
audio.PlayOneShot(batteryCollect);
var matchGUIobj = Instantiate(matchGUI, Vector3(0.15,0.1,0), transform.rotation);
matchGUIobj.name = "matchGUI";
}
}
function lightFire() {
fireOn = true;
var campfire : GameObject = GameObject.Find("campfire");
var campSound : AudioSource = campfire.GetComponent(AudioSource);
campSound.Play();
var flames : GameObject = GameObject.Find("FireSystem");
var flameEmitter : ParticleEmitter = flames.GetComponent(ParticleEmitter);
flameEmitter.emit = true;
var smoke : GameObject = GameObject.Find("SmokeSystem");
var smokeEmitter : ParticleEmitter = smoke.GetComponent(ParticleEmitter);
smokeEmitter.emit=true;
Destroy(GameObject.Find("matchGUI"));
}
@script RequireComponent(AudioSource)
I really cant see what im doing wrong, any help at all would be much appreciated.
asked
Mar 21 '12 at 01:33 PM
iJoshJenkins
0
●
1
●
1
●
1
Hint: All I see here is a poorly formatted wall of text. Fix the formatting first or no one will bother deciphering your question.
Well the error occurs in the lightfire() function, one of the variable returns null, add some Debug.Log() to figure out which. The error mentions line 97 so that would/could be: flameEmitter.emit = true;. Are you sure that the FireSystem GameObject has a ParticleEmitter component?