x


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.

more ▼

asked May 19 '12 at 05:03 PM

obliviousart gravatar image

obliviousart
0 2 2 3

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.

May 19 '12 at 06:11 PM obliviousart
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered May 19 '12 at 11:32 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

I'm getting an error saying "No overload for method 'Emit' takes '0' arguments" when I build out the script.

May 20 '12 at 12:02 AM obliviousart

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.

May 20 '12 at 12:03 AM Bunny83

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.

May 20 '12 at 12:45 AM obliviousart

It seems you're not familiar with the very basics of Unity, so i suggest you read the documentation carefully.

May 20 '12 at 01:09 AM Bunny83

This error occurs when you define the variable fireWork as ParticleSystem - define it as ParticleEmitter instead, 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)

May 20 '12 at 01:21 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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();

more ▼

answered May 19 '12 at 07:04 PM

Berenger gravatar image

Berenger
11k 12 19 53

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 mind, I see that doesn't work haha

May 19 '12 at 07:29 PM obliviousart
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3340
x337
x39

asked: May 19 '12 at 05:03 PM

Seen: 528 times

Last Updated: May 20 '12 at 01:29 AM