我希望我的问题不重复。 我需要在运行时更改胶囊对撞机的功能 我的代码:
Game_Controller.Database [m, n].Nute_M.AddComponent(typeof(CapsuleCollider));
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().center.y = 0.17f;Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().radius = 0.72f;
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().height = 0.64;
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().direction = 2;
但是我得到了
`Cannot modify a value type return value of `UnityEngine.CapsuleCollider.center'. Consider storing the value in a temporary variable`
错误。 请帮帮我。
答案 0 :(得分:0)
CapsuleCollider.center
是Vector3
。您无法修改在另一个类中声明为auto-property的Vector3
属性。复制它,修改y轴,然后将其分配回CapsuleCollider.center
。
此外,CapsuleCollider>().height
是一个浮点数,你需要在0.64f的末尾添加f
来进行编译。
//Make a copy
Vector3 center = Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().center;
//Modify the copy's y axis
center.y = 0.17f;
//Apply to the target object
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().center = center;
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().radius = 0.72f;
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().height = 0.64f;
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().direction = 2;
每次都不需要使用GetComponent
,因此您只需缓存CapsuleCollider
,因为它是一个类,它将存储引用:
//Get CapsuleCollider collider
CapsuleCollider cCollider = Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ();
//Make a copy
Vector3 center = cCollider.center;
//Modify the copy's y axis
center.y = 0.17f;
//Apply to the target object
cCollider.center = center;
cCollider.radius = 0.72f;
cCollider.height = 0.64f;
cCollider.direction = 2;