c# - 无法修改“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;
推荐阅读
- javascript - RactJs/Laravel - 无法提交表单的问题
- android - 我们检测到您的应用正在使用旧版本的 Google Play 结算。到 2021 年 11 月,所有应用更新必须使用版本 3 或更新版本
- javascript - Vue3、Vuex 不更新 DOM 元素
- html - 我们可以在 css 中使用类似 class 的函数,以便我们可以在任何元素中使用它,而无需为每个元素重复相同的样式吗?
- python - 为什么我对 2 位数字的 mod (%) 操作在 python 中返回零
- python - Python - 各种“原点”地理坐标和各种“目的地”地理坐标之间的距离
- spring - RemoteFileTemplate - 按日期拉取 sftp 文件
- python - 如何在scrapy-spider中使用全局定义的变量?
- java - 无论输入序列长度如何,都生成字母数字序列
- python - 在python中的每个元素后添加逗号