- Home /
Particle Emmitter not working
Hi, my problem is simple (please be patient, my knowledge of Unity isn't too extensive). I have a turret gameObject which has 2 pieces, the head and base. The turret has to aim automatically at "zombies" that walk through a trigger that has been attached to the parent object. There are two scripts attached to the head: AimTurret and TurretParticleController.
AimTurret:
var target : Transform;
var aiming = false;
function Aim(newTarget : Transform)
{
target = newTarget;
transform.LookAt(target);
aiming = true;
}
function Return(original : Transform)
{
target = null;
transform.LookAt(original);
aiming = false;
}
function isAiming()
{
return aiming;
}
TurretParticleController:
function Start ()
{ var aimControl : AimTurret = GetComponent(AimTurret);
//Establish link to particle emitter and deactivate emission.
var particles : Component[] = GetComponentsInChildren(ParticleEmitter);
for (var p : ParticleEmitter in particles)
{
p.emit = false;
}
//Once every frame update and check status.
while(true)
{
var isAiming = aimControl.isAiming();
for (var p : ParticleEmitter in particles)
{
p.emit = isAiming;
}
yield;
}
}
The aiming is working fine, and isAiming is also perfect. However when I run this, I get the following error:
MissingReferenceException: The object of type 'ParticleEmitter' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
I don't know what I am supposed to do to fix it, and my particle emitter isn't switching on and off like I want it to when the "Zombie" is inside the collider.
Thanks in advance for your help
Do you still get the error if you comment out the line "p.emit = false;" ? Because else this could be the same as lights, when you disable a light you cannot access it anymore. $$anonymous$$aybe if you disable particle emission from a particle emitter, it cannot be accessed again.
Im only guessing, thats why this is not in the answer section.
I have exactly the same problem. If I try to set the emit property to false, the emitter seems to disappear (after the last particle has died out, I believe). How are you supposed to be able to turn particle emitters on and off if this destroys the object? The enabled property works, however that just turns the GameObject on and off and leaves the particles suspended in mid-air, which looks silly. I hope there is a solution to this or it can be fixed.
Answer by Daveoh · Aug 21, 2011 at 12:44 PM
Ensure that Autodestruct is not enabled in the Particle Animator, which will destroy the entire GameObject when all particles have died out, shortly after turning off emissions.
http://unity3d.com/support/documentation/Components/class-ParticleAnimator.html
Thank you very much!
Didn`t let me to give you thumbs up, don`t know why :/
Answer by Joshua · Aug 17, 2011 at 10:22 PM
You probably have One Shot enabled on the particle emitter. If you have, it'll fire all particles at once, instead in a constant stream, and then destroy itself. Either turn this of, or keep creating a new one for every 'shot'.