Emitter Being Destroyed on Accident

I’ve got some code designed to turn a forklift “on.” When it’s on, an emitter emits and an animation plays so that the forklift goes tearing down the road kicking up dust.

It works pretty well, as I can start and stop the animation. When I first click on the object, it kicks up the dust (the emitter is working). But after stopping it and trying to restart, the animation works again, but no dust.

Unity gives the following error:
MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.

The script I’m using is included below. I didn’t think i was destroying the emitter; so does anyone have an idea of what’s happening?

var Lift: GameObject;
var emitter: GameObject;
private var emitting : boolean = false;
private var emitted : boolean = false;


//check for input
function OnMouseUp (){
      if (emitted)
         EmitOn();
      else
         EmitOff();
   }

//turning emitter on
function EmitOn() {

   	Lift.animation["C4D Animation Take"].speed=1;
   	emitting = true;
   emitter.particleEmitter.emit = true; 

   emitting = false;
   emitted = false;
}

//turning emitter off
function EmitOff() {

  	Lift.animation["C4D Animation Take"].speed=0;
   emitting = true;
   emitter.particleEmitter.emit = false; 
   emitting = false;
   emitted = true;
}

Sounds to me like you have Autodestruct checked in the particle animator. This causes the game object to automatically destroy itself when the emitter is turned off and the last particle is gone.

–Eric

Thanks Eric. You were so right…

I know you’ve got your problem solved, but two quick things:

First, for anyone who does want to autodestruct their particle systems, note that the flag will not destroy one-shot systems if they start out with emit enabled. You have to set emit or call Emit() from a script in order for it to autodestruct after its particles have disappeared.

Second, I noticed you have a member called “Lift”. Convention is to start member names with lowercase letters. Classes and functions start with uppercase letters, and you do not want to get in a situation where you’re confusing a member for a class or a function. Collider (the class) is a very different thing from collider (the member of Component).

Oh, OK…cool. Thanks for the tips.