0

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;

1 Answers1

-1

You cannot change the values of ParticleSystem.shape because it is not a variable indeed.. it is a 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
  • 429
  • 2
  • 10