Why Are We Using Var Variable?

we are using var emissionModule variable but we knows it’s a type of ParticleSystem then why we can’t change it. it’s giving a red Line Error. I’m a bit confused and by using this method we only able access it’s emission property but what if we want to access the particle other properties? do we need to write the same code by replacing emission property?.

1 Like

Hi!

Because we are not trying to get the reference of a Particle System, instead what we need is a reference to an Emission Module.

If you want to change that you’ll need to add the following:

ParticleSystem.EmissionModule emission = item.GetComponent<ParticleSystem>().emission;

As to why use var instead of being specific, it’s because it’s way too long making your code harder to read. Not even Unity uses the full declaration in the API, it’s just way too long and even confusing.

If you want to access the other components of the Particle System, yes, you would need to declare that somewhere else, because you can’t do something like this:

ParticleSystem pS = GetComponent<ParticleSystem>();
pS.emission.enabled = true;

That will mark you an error, so you do have to declare all of this stuff, there’s no shortcut around this.

Hope this helps!

4 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms