- Home /
Question by
Manco Capac · Dec 22, 2012 at 12:29 PM ·
particleemit
Particle System
How do u Play Particle System from a function? I have a Particle System(a) as the child of an object, and another one(b) as the child of (a). I want to start both of them at the same time. I have a function lightFire() in which a sound will play and the 2 particle systems would start. Could sb tell me the whole script to do this?
Comment
function lightFire(){
var barrel : GameObject = GameObject.Find("barrel");
var barrelSound : AudioSource = barrel.GetComponent(AudioSource);
barrelSound.Play();
var firesystem : GameObject = GameObject.Find("FireSystem");
var fireEmitter : ParticleEmitter = firesystem.GetComponent(ParticleEmitter);
fireEmitter.emit = true;
var smokesystem : GameObject = GameObject.Find("SmokeSystem");
var smokeEmitter : ParticleEmitter = smokesystem.GetComponent(ParticleEmitter);
smokeEmitter.emit = true;
}
I use the above code, the sound plays, but the Particle Systems do not play.
$$anonymous$$aybe your game Object names include spaces?
Answer by Next Beat Games · Dec 22, 2012 at 03:46 PM
I haven't tested this so may contain errors
using UnityEngine;
using System.Collections;
public class ExampleBehaviour : MonoBehaviour
{
// *** TODO: attach your GameObjects to these in
// your Inspector panel. More efficient so you dont have
// to keep looking for these objects every time you call
// LightFire();
public AudioSource barrelAudio;
public ParticleEmitter fireSystemEmitter;
public ParticleEmitter smokeSystemEmitter;
public void LightFire()
{
barrelAudio.Play();
fireSystemEmitter.emit = true;
smokeEmitter.emit = true;
}
}