Turning off child Particle Emitter, after destroying main object.

Hey guys!
I’m in the process of creating some fireballs for my game, however, everything doesn’t work out as i want them to do.

The Particle emitter is attached to the “Fireball” prefab, which is set to be destroyed on the collision, and at the same time, detaching the child objects (which is the Fire Particle Emitter, that’s following the fireball itself, to create the fire effect).

However, i want to “turn off” the emitters before destroying them, but this is where i can’t find the right solution.

This is how my current solution is, however, I’m pretty sure it isn’t optimal, as i only have 1 instance of the variable “HasCollided”, making all of my “flame” prefabs, which is the Particle Emitters Name, turn off, which isn’t optimal, if there’s still fireballs in the air at the time of some other fireball may trigger the script.

The following script is attached to my fireball “ball”.

static var HasCollided = false;


function OnCollisionEnter(collision : Collision)
{
      HasCollided = true;
    
	transform.DetachChildren();

    Destroy (gameObject);
 

    Debug.Log("Check");
}

and this script is attached to my “Fire” prefab, that contains 4 different elements - 3 particle and 1 light source. (However, this isn’t working, as i can’t turn off my emitters!)

var InnerCore : ParticleEmitter;
var Smoke : ParticleEmitter;
var OuterCore : ParticleEmitter;


function Update () {
	
		
	FlameStop();
}


function FlameStop()
{
if (Explosion.HasCollided == true)
	{
		InnerCore.emit = false;
		OuterCore.emit = false;
		Smoke.emit = false;
		yield WaitForSeconds(5);
		Destroy(gameObject);
		HasCollided = false;	
	}
}

So, can anyone shed some light on a fail, or just a different solution, so i might be able to have more fireballs in the air, than one.

Thanks in advance! :))

//Anders Schou - Trixxr.com

No one’s got an answer :frowning: ?

not sure why you’re

  1. detaching the emitters…
  2. wanting to turn them off
  3. using a static for hascollided

but if you have to do it, I might do it this way…

function OnCollisionEnter(collision : Collision)
{
    (transform.GetComponent("FirePrefab") as FirePrefab).FlameStop(); //note this is c#, ull have to convert it. Probably pretty similar
    

    Destroy (gameObject);

}

I’m detaching the emitters, to keep the trail of fire and create the illusion of the fireball “ball” having landed a specific place, which is why I’m letting it stay at the collision point for some time.
The point of turning it off, is to make the effect, that the fire is slowly dying, due to the main source (the ball) being gone.
The reason Hascollided is Static, is because I was trying to address it, in the 2nd script, which was attached to the emitter, and not the ball.

Does that make any sense, by any chance :slight_smile: ? I can see it can be a bit random, some of the things, but they’re done with good intention!
¨
And thank you! I will start fooling around with your solution tomorrow! Will bring some feedback, if everything works out just fine! :slight_smile: +1 to you, my friend.

I dont believe it needs to be static to be referenced from the other script, although im coming from c#…

anyway, get rid of the bool completely and do something like this…

function OnCollisionEnter(collision : Collision)
{
    (transform.GetComponent("FirePrefab") as FirePrefab).FlameStop(); //note this is c#, ull have to convert it. Probably pretty similar
    
transform.DetachChildren();
    Destroy (gameObject);

}

Never use static variables for something instance specific.

jlcnz’s approach is good, and is what I’d use - have the ‘ball’ call a function on the ‘fire’ script directly. In Javascript the syntax would be something like this (assuming your fire script is called fireScript.js, otherwise change what’s after the colon)

var fire : fireScript;


function OnCollisionEnter(collision : Collision)
{
    fire.FlameStop();
    transform.DetachChildren();

    Destroy (gameObject);
 
    Debug.Log("Check");
}

Note you’ll need to drag the object with the fire script onto the “fire” variable in the inspector.

The other approach is to broadcast a message, since broadcast is sent to child objects.

function OnCollisionEnter(collision : Collision)
{  
    gameObject.BroadcastMessage ("FlameStop", null);
    transform.DetachChildren();

    Destroy (gameObject);

    Debug.Log("Check");
}

Hope that helps!