Changing color of Particles C#


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*.color =;*
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?

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`).