-2
\$\begingroup\$

I get the following error every time I run the code:

error CS0120: An object reference is required to access non-static member `UnityEngine.Joint.breakForce'

Joint.breakForce = Mathf.Infinity;

How can I properly write this? I want to set the break force to Mathf.Infinity to render the joint unbreakable.

\$\endgroup\$
1
\$\begingroup\$

Looks like you just need to reference the object

private FixedJoint joint;
void Start()
{
    joint = GetComponent<FixedJoint>();//assuming the joint and script components are attached to same gameobject.
    joint.breakForce = Mathf.Infinity;
}
\$\endgroup\$
0
\$\begingroup\$

Here Joint is a class. If you have an object called Joint, rename it to lowercase joint, since that is the proper naming style in C#. Your code then should become:

joint.breakForce = Mathf.Infinity;
\$\endgroup\$
  • \$\begingroup\$ And how do I properly write this? FixedJoint.breakForce = Mathf.Infinity; \$\endgroup\$ – That's me. Oct 28 '15 at 13:43
  • \$\begingroup\$ Because I get error CS0103: The name `joint' does not exist in the current context \$\endgroup\$ – That's me. Oct 28 '15 at 13:43
  • \$\begingroup\$ Because there should be an object called 'joint', which you want to make unbreakable, but here you're trying to set the break force to a class called 'Joint' which doesn't make sense. Adding more context code into your question may help resolving the issue. \$\endgroup\$ – Maks Maisak Oct 28 '15 at 13:47
  • \$\begingroup\$ My other question and what I want to achieve: stackoverflow.com/questions/33389802/… \$\endgroup\$ – That's me. Oct 28 '15 at 13:53
  • \$\begingroup\$ Also, read the comments in the other question. \$\endgroup\$ – That's me. Oct 28 '15 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.