- Home /
How to enable a ParticleEmitter in scripting
Help, I need to make a particle emitter emit at MouseClick. I have gotten everything ready except the actual thing. I need to make the ellipsoid particle emitter emit, I really need some help. I have a main gameobject called gunprototype, and another object in it called maingun. The maingun object has the models etc. I have created a gameobject in the main 'gunprototype' object called Gun, and in Gun there is a ellipsoid particle emitter and the script. the problem is, it is giving me code errors like "cannot convert from UnityEngine.GameObject to UnityEngine.ParticleEmitter". I tagged the emitter with the tag "Seagull".
function Update () { var goEmitter : GameObject; var emitter : GameObject; goEmitter = GameObject.FindGameObjectsWithTag("Seagull");
if (Input.GetMouseButtonDown (0))
{
BroadcastMessage("Firing!");
goEmitter.Emitter.emit = true;
}
}
Answer by · Sep 23, 2010 at 10:02 AM
The error is because you're declaring 'emitter' as a GameObject, and then referring to it as a particleEmitter. I've updated your script and it should work (assuming that the gun is the only object with the "Seagull" tag (be aware that you can create your own tags too).
A few notes:
- Put your 'Find' function in Awake() or Start() so it's only looked up once, and then cached.
- FindGameObjectsWithTag returns an array of GameObjects, and you were only accessing the first.
- You don't need Emitter. "particleEmitter" refers to the particleEmitter component on the gameObject.
- Do you need to BroadcastMessage("Firing!") ? This attempts to call Firing!() on all GameObjects...
Hope this helps.
function Awake() { var goEmitter : GameObject = GameObject.FindWithTag("Seagull"); }
function Update () { if (Input.GetMouseButtonDown (0)) { BroadcastMessage("Firing!"); goEmitter.particleEmitter.emit = true; } }
No worries. Remember to vote up any answers that helped you, and if it solves your question, mark it correct by clicking on the tick beneath the up/down arrows. Cheers
Your answer
Follow this Question
Related Questions
Errors in Vector2 variables 1 Answer
Particle Emitter error, Missing reference exception. 1 Answer
why this does not work (Lists) 3 Answers
Casting error on multiple emitters 3 Answers