Object reference not set to an instance of an object

I’m a beginner in Unity, I’m following a tutorial book and everything is going well so far, except for this problem. I’m getting the NullReferenceException for this code:

function lightFire()
{
	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"));
	
}

The error points at this line:

	flameEmitter.emit = true;

The lightFire function gets called here:

function OnControllerColliderHit(hit : ControllerColliderHit)
{
	
	if(hit.collider.gameObject == GameObject.Find("campfire"))
	{
		if(haveMatches)
		{
			haveMatches = false;
			lightFire();
		}
		else
		{
			TextHints.textOn = true;
			TextHints.message = "whatever...";
		}
	}
	
}

What happens when I press the Play button: only the sound plays but nothing happens with the particles and the GUI doesn’t disappear.
Any help would be appreciated.

you’re missing something… quite literally

var flameEmitter : ParticleEmitter = flames.GetComponent(ParticleEmitter);

aka “get the ParticleEmitter from flames”… check that there is one

var flames : GameObject = GameObject.Find("FireSystem");

aka “find the object called FireSystem”… check that it exists and is spelled exactly “FireSystem” and not firesystem, fireSystem etc.

Could you explain this? Check that there’s a Particle Emitter?

Otherwise, the spelling is correct. It’s as you said, something seems to be missing but I don’t know what or where.

Your GameObject named FireSystem doesn’t have a ParticleEmitter component attached to it.

To prove it, put this line right before the line causing the error

Debug.Log("Did we find a particle emitter? " + (flameEmitter != null));

I got it. The book is a bit old (for Unity 3.5 or something) and the particle system has changed, so I added the Ellipsoid Particle Emitter manually and I no longer get an error, and it now says “True” to the “Did we find a particle emitter?” debug.
Now it still doesn’t work though; the particles are either on or off, the script doesn’t change that. Only the sound works fine but the particles and the Destroy/GUI don’t work and the text hint stays the same before and after picking up the matches.

Am I missing something else? And do I have to remove the Particle System and leave only the Ellipsoid Particle Emitter?

I’d use the new system and get the ParticleSystem component and then use the Play() method instead of trying to screw around with individual emitters.