I’m a new Unity user and have been reading Unity Game Essentials by Will Goldstone, great book btw!
I am at the chapter in the book, kindle version, where a campfire particle and smoke system are implemented and activated in a script when the player has some matches and touches the campfire model.
Here is the code that is called when this occurs. The audio plays okay.
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”));
TextHints.textOn=true;
TextHints.message=“You lit the fire,you’ll survive, well done!!”;
yield new WaitForSeconds(5);
Application.LoadLevel(“Menu”);
Here is the error, which occurs on the “flameEmitter.emit=true;” line
NullReferenceException: Object reference not set to an instance of an object
PlayerCollisions+$lightFire$56+$.MoveNext () (at Assets/Scripts/PlayerCollisions.js:128)
UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
PlayerCollisions:OnControllerColliderHit(ControllerColliderHit) (at Assets/Scripts/PlayerCollisions.js:65)
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)
The game doesn’t crash, which is a good thing I guess. LOL
I apologize in advance if the code isn’t formatted correctly.
Anyone who is familiar with the book and/or this code who can help me would be great. I have emailed Mr. Goldstone but haven’t gotten a reply as of yet.
I will check but the chapter was about particle systems so it should have an emitter attached. How can I make sure it has one, just check the FireSystem in the inspector?
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"));
TextHints.textOn=true;
TextHints.message="You lit the fire,you'll survive, well done!!";
yield new WaitForSeconds(5);
Application.LoadLevel("Menu");
}
I deleted the section of the code that is supposed to start the fire particle system to see if I get the same error for the smoke section, and I do, which I expected.
Could the error be a result of the fact that A) I’m using Unity 4 and the book is based on Unity 3.X? and B) that both particle systems are a child of the campfire object and thus aren’t accessible via these commands?
Bear in mind that the ParticleEmitter component is part of the legacy particle system, which is still present in Unity but not recommended for new development. You might want to check that you’re not accidentally using the newer ParticleSystem component.