Unity 中的 FixedJoint 出错?

Error with FixedJoint in Unity?

我每次 运行 代码时都会收到以下错误:

错误 CS0120:需要对象引用才能访问非静态成员“UnityEngine.Joint.breakForce”

Joint.breakForce = Mathf.Infinity;

我怎样才能正确地写这个?可以将断裂力设置为 Mathf.Infinity 以使关节牢不可破。

每当您看到错误时:

An object reference is required to access non-static member [...]

这强烈建议您在实际应该使用 class 实例的地方使用 class 名称。在这种情况下,它表示您需要 Joint 的实例(或者更确切地说,FixedJoint,对吗?)以更改其 breakForce 值。

要解决此问题,您首先需要使用 GetComponent() 从当前 GameObject 中检索 FixedJoint 的实例,然后才设置其 breakForce。例如,如果您在 Start() 方法中执行此操作:

Start() {
    GetComponent<FixedJoint>().breakForce = Mathf.Infinity;
}

希望这对您有所帮助!如果您有任何问题,请告诉我。