`UnityEngine.GameObject' does not contain a definition for `hingeJoint2D'

Hi everyone

I’m trying to attach the HingeJoint2D of a game object to another game object’s Rigidbody2D at runtime. However, I get the following error message:

error CS1061: Type UnityEngine.GameObject' does not contain a definition for hingeJoint2D’ and no extension method hingeJoint2D' of type UnityEngine.GameObject’ could be found (are you missing a using directive or an assembly reference?)

GameObject LinkObject = (GameObject) Instantiate(LinkPrefab, (playerA.transform.position - distance/2), Quaternion.identity);
LinkObject.hingeJoint2D.connectedBody = playerA.rigidbody2D;

The prefab I’m instantiating clearly has a 2d HingeJoint attached, all other physics components are 2d as well. How can I access that component from script?

thanks in advance for your help

that’s because LinkObject is GameObject and not hingeJoint2D

try doing this:

    GameObject LinkObject = (GameObject) Instantiate(LinkPrefab, (playerA.transform.position - distance/2), Quaternion.identity);
    LinkObject.GetComponent<hingeJoint2D>().hingeJoint2D.connectedBody = playerA.rigidbody2D;

OR some people do it in more lines I do it in more only when I need to do it more than only just once.

    GameObject LinkObject = (GameObject) Instantiate(LinkPrefab, (playerA.transform.position - distance/2), Quaternion.identity);
    hingeJoint2D Something = LinkObject.GEtComponent<hingeJoint2D>();
    Something.hingeJoint2D.connectedBody = playerA.rigidbody2D;

OR it could even be only:

    Something.connectedBody = playerA.rigidbody2D;

I’m not sure I’m not in this 2D joiunts and stuff, or you could even do 1 your self I don’t know, …

IF hingeJoint2D even exists, … AND also if it’s everything correctly linked together.