首页 > 解决方案 > 无法修改“ParticleSystem.shape”的返回值,因为它不是变量

问题描述

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

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

标签: c#unity3d

解决方案


您无法更改 的值,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;

推荐阅读