- Home /
Question about logical flow of a script
What I'm trying to do is trigger the prefab FireWorks from the particles folder when a rigidbody enters a trigger area, which is a box with "is Trigger". I am new to scripting, I've always been the modeler/animator. I'm not looking for specific code,I am looking for the logical flow of this example. I know I'll need a script that is attached to the trigger area. I know I'll need to somehow call on the particleEmitter component of FireWorks in that script to turn it on when the area is triggered. What I don't understand is how this all fits together.
I understand that I'll first start with a class
public class TriggerGoal : Monobehaviour
I'm guessing that in that class I'll need a variable to save the info from the FireWorks particle emitter state. But I'm not sure how that works or if that's how it is done.
I know I'll need the functionality for the actual trigger
void OnTriggerEnter(Collider other)
But after that I am lost. I've really tried finding this on my own but most questions answered here basically assume you understand how things work together, I believe.
Can you help with calling the FireWorks particleEmitter enable/disable? After more research that's pretty much the only thing I'm having trouble with now.
Answer by aldonaletto · May 19, 2012 at 11:32 PM
Detailed sequence:
1- Drag the Fireworks effect to the scene and position it where you want; uncheck its field Emit in the Inspector, or the fire work will keep exploding forever;
2- Attach the script below to the trigger object;
3- Select the trigger object, then in the Inspector click the field Fire Work in the component Trigger Goal: this allows selection of the Fireworks effect you've placed in scene;
Presto! It's ready to play!
public class TriggerGoal : MonoBehaviour;{
public ParticleEmitter fireWork; // this will appear as Fire Work in the Inspector
void OnTriggerEnter(Collider other){ fireWork.Emit(); } } NOTE: The Fireworks effect will explode whenever any rigidbody or CharacterController enters the trigger. If you want to fire the effect only when certain objects enter the trigger, you may compare other.tag to some specific tag:
void OnTriggerEnter(Collider other){
if (other.tag == "SomeTag"){ // only SomeTag tagged objects will work
fireWork.Emit();
}
}
Remeber to tag the objects accordingly, if you want to use this alternative.
I'm getting an error saying "No overload for method 'Emit' takes '0' arguments" when I build out the script.
The usually simpler way for point 3 is to grab the fireworks gameobject in the hierarchy panel and drop it on the fireworks variable of the trigger script in the inspector.
I've tried dragging and dropping but no go. I noticed if I put a number in, as in fireWork.emit(1);, the error goes away but the script still doesn't work.
It seems you're not familiar with the very basics of Unity, so i suggest you read the documentation carefully.
This error occurs when you define the variable fireWork as ParticleSystem - define it as ParticleEmitter ins$$anonymous$$d, like in my answer. ParticleSystem is the new Shuriken particle generator, while ParticleEmitter is the old style particle generator, which is used in the Fireworks prefab (at least in the Particles Unity package)
Answer by Berenger · May 19, 2012 at 07:04 PM
To emit particle, you will need a reference of the particle system component. The easiest to do that is to declare the variable as public and of type ParticleSystem (ParticleEmitter if it's not the new particle system). It's going to look like that :
// the first letter of fireWork being a lower case is just a convention.
public ParticleSystem fireWork;
Then, you need to detect th collision. When the physic engine detect a collision, it sends a message to the game object being collided. If its collider is a trigger, that message is OnTriggerEnter. By implementing that function, you can receive the message. Now you need to make the emitter emit. For the new system, it would be fireWork.Play(); For the old one, fireWork.emit = true or fireWork.Emit();
Ok, so I need to already have the fireworks placed where I want it with its emitter disabled, and named fireWorks, correct? Or am I misunderstanding the concept of the referencing of the ParticleSystem?
Edit: never $$anonymous$$d, I see that doesn't work haha
Your answer
Follow this Question
Related Questions
C# - Problem with trigger that won't activate 1 Answer
OnTriggerEnter questions 2 Answers
Door Sound Script Help 1 Answer
OnTriggerEnter Glitch 1 Answer
Where to attach a script? 1 Answer