- Home /
Multiple particle systems in one script
Hi!
I currently have four gameobjects with particle systems attatched to them
public ParticleSystem red, blue, green, yellow;
and then I have
void Start () {
green = GetComponent <ParticleSystem> ();
red = GetComponent <ParticleSystem> ();
yellow = GetComponent <ParticleSystem> ();
blue = GetComponent <ParticleSystem> ();
}
How do I know that each variable is connected to the correct component? Is there a better way to solve this? I need to be able to activate the emissions separate from each other. Thank you very much!
You could always do GetComponents()
ParticleSystem[] particles = GetComponents<ParticleSystem>();
green = particles[1];
red = particles[2];
Answer by Commoble · Mar 19, 2017 at 08:30 PM
When you have multiple components of the same type attached to an object, you can't use GetComponent to find them; you have to put public fields in your script like this:
public ParticleSystem greenParticles;
public ParticleSystem redParticles;
public ParticleSystem yellowarticles;
public ParticleSystem blueParticles;
This will create fields in your script's Inspector that you can drag your components to. You can get more details on this subject here and here.
Thank you! I'm new to unity so I had missed to connect them through the inspector. There was no need to put them on separate lines though public ParticleSystem red, blue, green, yellow; still works.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113519im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to render a particle system with a preset particle system in a prefab? 0 Answers
How do you change size of an object by its units rather than scale? 1 Answer
Bullets follow the ship in 2D Space Shooter game 2 Answers
2D Tilemap Gameobject Brush Script for Editor 0 Answers
Particle System error in Unity 3.5 1 Answer