- Home /
How do I both "find" this and stop it from emitting?
With the following code, I get one of two problems:
If I declare `inner` and `outer` as ParticleEmitters, it tells me it can't convert a GameObject to a ParticleEmitter when I use the `FindGameObjectWithTag()` method.
If I declare them as GameObjects, then that works, but then it tells me there's no `emit` method on GameObject.
Here's the code, currently in the first form. How can I accomplish this?
var inner: ParticleEmitter;
var outer: ParticleEmitter;
function Start () {
inner = GameObject.FindGameObjectWithTag("flame_inner");
outer = GameObject.FindGameObjectWithTag("flame_outer");
inner.emit = false;
outer.emit = false;
}
Answer by aldonaletto · Nov 30, 2012 at 01:14 AM
The GameObject provides access to some popular components, including the ParticleEmitter - you can use it when finding the GameObject:
function Start(){
// FindWithTag does the same as FindGameObjectWithTag:
inner = GameObject.FindWithTag("flame_inner").particleEmitter;
outer = GameObject.FindWithTag("flame_inner").particleEmitter;
inner.emit = false;
outer.emit = false;
}
Answer by DecipherOne · Nov 30, 2012 at 01:16 AM
The ParticleEmitter has to be instanced in order to use it. In other words, with what you have you aren't actually assigning an object. If you have the particles emitters in your scene already, you can simply declare a public game object, assign the object in your scene using the inspector in the editor. Then you will want to access the particle emitter component. Also, if the functionality is something that you are going to use in multiple spots, I'd recommend just creating a generic script that you can reuse. In C# it may look something like this :
using UnityEngine;
using System.Collections;
public class SetParticles : MonoBehaviour {
public GameObject myEmitter;
ParticleEmitter myRef;
public bool emit;
void Start ()
{
myRef = myEmitter.GetComponent ("ParticleEmitter") as ParticleEmitter;
myRef.emit = emit;
}
// Update is called once per frame
void Update () {
}
}
You then would simply assign the GameObject with a ParticleEmitter through the inspector. If you are doing things dynamically, it gets a little bit more involved, but still the same general idea.
Your answer
Follow this Question
Related Questions
store GameObject.FindGameObjectsWithTag in an array 2 Answers
One particle pulse when gameobject is destroyed. 2 Answers
Need help finding the closet player to an enemy 2 Answers
Getcomponent error? 1 Answer