x


Changing color of Particles C#

Hi,

What I am trying to do is change the color of my particles which is a prefab attached to my game object. Everytime I press the 'h' key on the keyboard the particle system instantiates. I am trying to change the colors of each particle from white to red by iterating through the array.

I've tried to do this with the following code within an OnTriggerStay function:

//... private Particle[] PlayerParticles; public GameObject ParticlePrefab; // added to the player under its script private GameObject instantiated;

if (Input.GetKey("h") && instantiated == null)
{ instantiated = (GameObject)Instantiate(ParticlcePrefab, transform.position, Quaternion.identity);
PlayerParticles = particleEmitter.particles; // Error for (int i = 0; i < PlayerParticles.Length; i++) { PlayerParticles[i].color = Color.red; } particleEmitter.particles = PlayerParticles;

}

Getting the error:

MissingComponentException: There is no 'ParticleEmitter' attached to the "Player" game object, but a script is trying to access it. You probably need to add a ParticleEmitter to the game object "Player". Or your script needs to check if the component is attached before using it.

I know for a fact that the particle emitter is attached to the gameobject, player. Why would the particle emission happen upon pressing 'h' if it wasnt?

more ▼

asked May 28 '10 at 09:51 PM

lampshade gravatar image

lampshade
391 84 92 109

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

When you instantiate something, you create a new game object. You cannot instantiate a component and place it on the current game object.

You need to either insert your particle emitter onto your game object in the editor, and then turn it on and off as needed at runtime, or create the particle emitter entirely with code (using AddComponent).

more ▼

answered May 28 '10 at 10:00 PM

qJake gravatar image

qJake
11.6k 43 78 161

Would I start by making a new script like, ParticleEmitter and addthat script to the player game object?

May 28 '10 at 10:19 PM lampshade

No, you can't make a script called "ParticleEmitter". That's a reserved word. I'm talking about adding the Emitter component to your current game object, using the Components menu at the top of Unity.

May 28 '10 at 10:42 PM qJake

Alright, I added the component (I guess I was just being stubborn, thinking I was right) and I am not getting the error, which is great. But I am still left with needing to change the particle color from white to red.

May 28 '10 at 11:06 PM lampshade

Would I need to add the Particle Animator component instead? I am not getting any color functionality with just the Ellipsoid Particle Emitter.

May 28 '10 at 11:16 PM lampshade

That does not work b/c the code is wanting the Ellipsoid Particle Emitter, like I have.

May 28 '10 at 11:17 PM lampshade
(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:

x643
x509

asked: May 28 '10 at 09:51 PM

Seen: 2455 times

Last Updated: May 28 '10 at 10:48 PM