Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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;
}
share|improve this answer

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;
share|improve this answer
    
And how do I properly write this? FixedJoint.breakForce = Mathf.Infinity; – user5497945 2 days ago
    
Because I get error CS0103: The name `joint' does not exist in the current context – user5497945 2 days ago
    
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. – Maksim Maisak 2 days ago
    
My other question and what I want to achieve: stackoverflow.com/questions/33389802/… – user5497945 2 days ago
    
Also, read the comments in the other question. – user5497945 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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