Creating fire particle in code

Hello everybody,

I’m just starting with unity and so far i’m loving it! Right now I’m stuck with this problem and hope somebody can help me with it.

I have imported a tree model and i emit the particles from the Fire1 prefab when you click on the tree. But now, I want to create the Fire1 prefab on runtime when i click somewhere and start the fire where i clicked.

I’m already doing raycasting so positioning will not be a problem, but I have no idea how to create the Fire1 prefab in code. Can somebody help me with this?

Declare a variable in your script to hold the fire prefab, and then assign the prefab to that variable in the editor:

public var fire : GameObject;

Then, when you want to set fire to the tree:

Instantiate(fire, firePosition, Quaternion.identity);

where firePosition is the place you want to put the flames.

Passing Quaternion.identity for the rotation, as above, will create the fire unrotated. If you have a specific rotation you want to use instead, replace Quaternion.identity with the correct rotation.

If you want the fire to disappear after a little while, you can use:

Destroy(Instantiate(fire, firePosition, Quaternion.identity), fireTime);

and replace fireTime with the amount of time you want the fire particle effect to last. This will create the effect, then tell Unity to destroy it after a specified amount of time.

For more info: Instantiating Prefabs