Simpler Code in the ForEachLoop

This could be for educational purposes but Ricks code is unnecessary complicated in the beginning. If you just store the laser as a Particlesystem and not a Gameobject you can just write

            foreach (ParticleSystem laser in lasers)
            {
                var emission = laser.emission;
                emission.enabled = false;
            }
5 Likes

Hey hey, when i try and implement your solution, i get an error stating:

Error CS0030 The type UnityEngine.GameObject cannot be converted to UnityEngine.ParticleSystem.

I’m suspecting you have stored the lasers as gameobjects and not particle systems. You propably have

[SerializeField]} GameObject lasers; in the code.

you could change it to

[SerializeField]} ParticleSystem lasers;

It’s a type mismatch,

You could also try var emission = laser.GetComponent().emission;

if you’ve stored it as something else.

1 Like

Oooohh!! Okay - yeah i declared an GameObject array. Pretty cool! Thanks!

Glad to be of service! :smiley:

Privacy & Terms