- Home /
Problem trying to set "connected body" of Fixed Joint via script upon build
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
Then ive put this code to check:
Debug.Log(obj1.GetComponent("FixedJoint"));
Debug.Log(fixJoint.connectedBody);
and ive got:
Obj1 (UnityEngine.FixedJoint)
Obj2 (UnityEngine.Rigidbody)
So everything seems to be working fine on editor mode ?! Any ideas?
I`ve got the same problem. Every time I try to assign "connectedBody" via code, it says that "Joint2D" can not access non-static member "connectedBody". Is there a way to solve this problem? :c
Answer by DFLY · Jun 27, 2014 at 03:33 PM
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;
}
}
Hope this helps someone...
Answer by sneftel · Apr 29, 2011 at 09:35 PM
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;
Your answer
Follow this Question
Related Questions
Joint connected body not working 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Multiple connected body on fixed joint 1 Answer
Rigidbody Sliding UP Incline 0 Answers
Force to Velocity scaling? 2 Answers