1

After upgrade my project i got this error." Cannot modify the return value of 'ParticleSystem.shape' because it is not a variable" anyone know what is wrong with the code.

gameObject2.GetComponentInChildren<ParticleSystem>().shape.radius = objectNode2.GameObject.GetComponent<CharacterController>().radius;

Scott Blank
  • 39
  • 2
  • 4

1 Answers1

-1

You cannot change the values of ParticleSystem.shape because it is not a variable indeed.. it is a get only property. Properties that return a struct don't allow changes to that struct's fields.

The following code would provide the same error.

Vector2 vec;
Vector2 Vec { get { return vec; } }

void ChangeVecX() { Vec.x = 2; }

For more info about the differences between variables and properties, refer to this post: https://stackoverflow.com/a/4142957/9433659

This is how you should do it:

var ps = gameObject2.GetComponentInChildren<ParticleSystem>();

// Copy the value of the property
var newShape = ps.shape;

// Make modifications
newShape.radius = objectNode2.GameObject.GetComponent<CharacterController>().radius;

You would normally have to do the following in a normal property that returns a struct, but Unity does a bit of a special thing for ParticleSystem using Pointers, so you don't have to.

// Assign the new value to the property
ps.shape = newShape;
Lyrca
  • 528
  • 2
  • 15
  • Error CS0200 - Property or indexer "ParticleSystem.shape" cannot be assigned to -- it is read only – emredesu Dec 27 '22 at 18:25
  • 1
    @emredesu if you actually took the time to read the answer instead of copy-pasting the code you'd see I'm saying that you DON'T have to do that part. – Lyrca Dec 29 '22 at 00:07