0

In unity, when I try to do the following;

gameObject1.GetComponent<ParticleSystem>().main.startSize = transform.localScale.x / 5;

It says;

Cannot change the return value of "ParticleSyste.main" because it is not a variable.

But if I try to do the following;

var particleSystemMain = gameObject1.GetComponent<ParticleSystem>().main;
particleSystemMain.startSize = transform.localScale.x / 5;

it works. Why cannot I directly modify a propert's property?

Also, Unity documentation does this in the second way: https://docs.unity3d.com/ScriptReference/ParticleSystem.MainModule-startSize.html

M. Azyoksul
  • 1,580
  • 2
  • 16
  • 43

1 Answers1

3

When you assign main to var particleSystemMain, you actually get a copy of it. It's not that the action is not technically possible, but it will not change the main in your partical system, and your modified copy is not saved under any form, so c# straight up forbids it.

See this similar question for a slightly deeper explanation

Tomer Shahar
  • 343
  • 1
  • 8