无法修改"ParticleSystem.shape"的返回值,因为它不是变量



升级我的项目后,我收到此错误。无法修改"ParticleSystem.shape"的返回值,因为它不是变量"任何人都知道代码出了什么问题。

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

你不能更改ParticleSystem.shape的值,因为它确实不是一个变量......它是一个只获取的属性。返回结构的属性不允许更改该结构的字段。

以下代码将提供相同的错误。

Vector2 vec;
Vector2 Vec { get { return vec; } }
void ChangeVecX() { Vec.x = 2; }

有关变量和属性之间差异的更多信息,请参阅这篇文章:https://stackoverflow.com/a/4142957/9433659

这是你应该怎么做的:

var ps = gameObject2.GetComponentInChildren<ParticleSystem>();
// Copy the value of the property
var newShape = ps.shape;
// Make modifications
newShape.radius = objectNode2.GameObject.GetComponent<CharacterController>().radius;

您通常必须在返回结构的普通属性中执行以下操作,但 Unity 在使用指针ParticleSystem做了一些特殊的事情,因此您不必这样做。

// Assign the new value to the property
ps.shape = newShape;

相关内容

最新更新