Im trying to set the component connected body of a Fixed Joint via script but i get a "connectedBody' is not a member of 'UnityEngine.Component" error when trying to build.
Ive tried something like this:
obj1.AddComponent("FixedJoint");
var fixJoint = obj1.GetComponent("FixedJoint");
fixJoint.connectedBody = obj2.rigidbody; //ERROR HERE
Hi all. I know this is WAY later than the question. But I came across the same problem. I finally stubbled across the answer. For some reason you have to explicitly declare your rigidbody and fixed joint variables at the beginning and NOT within a function. I don’t know why but just declare them at the beginning.
Here is my script. It turns children to rigid bodies and fix joins them:
#pragma strict
private var rig: Rigidbody; // Have to explicitly declare this HERE
private var fix: FixedJoint;// Also explictly declare this HERE
private var lastName: String;
private var childCount: int;
function Start () {
lastName = "Cube4"; // Just a placeholder. Cube4 is my platform.
childCount = 0;
for (var child : Transform in gameObject.transform){
var childName : String = child.name;
rig = child.gameObject.AddComponent ("Rigidbody");
if (childCount != 0){
fix = child.gameObject.AddComponent ("FixedJoint");
fix.connectedBody = gameObject.Find (lastName).GetComponent ("Rigidbody");
}
Debug.Log ("childName = " + childName);
Debug.Log ("lastName = " + lastName);
Debug.Log ("rig = " + rig);
Debug.Log ("fix = " + fix);
if (childCount != 0) {
Debug.Log ("fix.connectedBody = " + fix.connectedBody );
}
lastName = childName;
childCount = childCount +1;
}
}
Is that C#? If so, then you need to either cast the result of GetComponent, or (better) use the generic version of GetComponent. Incidentally, AddComponent returns the created component, so you can do all that in one step. Like so:
var fixJoint = obj1.AddComponent<FixedJoint>();
fixJoint.connectedBody = obj2.rigidbody;