- Home /
(javascript) Activating an emitter through script
Hello, I have a particle emmiter in my scene called "confetti" and I'm trying to get it to activate when the player walks into a trigger. The script I'm trying to use is:
public var confettiEmmiter : GameObject = GameObject.Find("confetti");
function OnTriggerEnter (other : Collider)
{
if(other.tag == "Player")
confettiEmitter.particleEmitter.emit = true;
}
I'd like this one emmitter to go off when the player enters the trigger, but I keep getting an error that says "Unknown Identifier 'confettiEmmitter'. What am I doing wrong?
Answer by cassius · Nov 15, 2012 at 02:43 AM
Here's some code to get you going :)
public var confettiEmitter : ParticleSystem;
function Start() {
confettiEmitter = GameObject.Find("Confetti").GetComponent(ParticleSystem);
}
function OnTriggerEnter (other : Collider) {
if(other.tag == "Player") {
confettiEmitter.Emit(30); //emits 30 particles
}
}
Answer by iHaveReturnd · Nov 14, 2012 at 08:28 PM
Are you using the new particle system? Shuriken?
It gets handled differently than the old commands. Check out this thread: http://answers.unity3d.com/questions/235724/turn-on-off-shuriken-particle-system.html
That seems to be part of the problem, as I was coding for the old system, but I'm getting an error that says: "Object reference not set to an instance of an object" in reference to the line: "cfSystem.enableEmission = true;" in my current code, which is this:
var cfSystem : ParticleSystem;
cfSystem = gameObject.GetComponent("Confetti");
function OnTriggerEnter (other : Collider) { if(other.tag == "Player")
cfSystem.enableEmission = true;
}
cfSystem = gameObject.GetComponent("Confetti")
Is "Confetti" somehow your particleEmitter? I think you want to get the Particle Emitter component, no?
Yes, "Confetti" is the name of the particle emmiter I have in the scene.
Just to clarify, "Confetti" is the component's name and not the gameObject's name, correct? If it's the gameObject then you'll need to get the particleEmitter component of the Confetti gameObject.
I think it's the gameObject. Confetti is the name of the particle emmitter I have in the scene. I'm not very experienced with unity program$$anonymous$$g, sorry
Your answer
Follow this Question
Related Questions
Check collision of childs Trigger in Parents script [JS] 1 Answer
Get all particles within trigger collider? 1 Answer
Changing Color of Particles in Javascript 1 Answer
My Particles Stop ? 1 Answer
Setting Scroll View Width GUILayout 1 Answer