x


Chapter 7 Lighting the Fire

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.

more ▼

asked Mar 21 '12 at 01:33 PM

iJoshJenkins gravatar image

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.

Mar 21 '12 at 03:09 PM by0log1c

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?

Mar 21 '12 at 04:20 PM by0log1c
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

¡Hi!

I have the solution for this.

The deal is that our "Particles" doesn't are a Emitter, they are a System.

The code of the lightFire(), must be:

function lightFire(){

       var campfire : GameObject = GameObject.Find("campfire");
       var campSound : AudioSource = campfire.GetComponent(AudioSource);
       campSound.Play();

       var flames : GameObject = GameObject.Find("FireSystem");
       var flameEmitter : ParticleSystem = flames.GetComponent(ParticleSystem);
       flameEmitter.Play();

       var smoke : GameObject = GameObject.Find("SmokeSystem");
       var smokeEmitter : ParticleSystem = smoke.GetComponent(ParticleSystem);
       smokeEmitter.Play();

       Destroy(GameObject.Find("matchGUI"));

}

Greetings

more ▼

answered Oct 16 '12 at 07:45 PM

santuario gravatar image

santuario
1

Hi santuario, thank you for your script,

it does not emit still :(

however i get no errors, and the Matchbox texture does destroy itself so yay. :-/

edit

AHAA!! Got it, cheers santuario, all I had to do was, A) turn the emitter back on (dam you out of date tutorial) and B) untick "Play On Awake"

Thank you.

Oct 17 '12 at 01:28 PM LexGear
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x149

asked: Mar 21 '12 at 01:33 PM

Seen: 555 times

Last Updated: Oct 17 '12 at 01:33 PM