- Home /
Question by
BobbyDoogle · Mar 28, 2014 at 11:56 PM ·
rigidbodyconstraint
Set all Children Rigidbody Constraints C#
I'm trying to trigger removal of all constraints on children rigidbodies when an object collids with the parent trigger. I'm using the following code:
void OnTriggerEnter(Collider col){
if(col.tag=="EnemyType1"){
foreach (Transform child in transform)
{
Debug.Log ("im a child");
if (transform.gameObject.rigidbody!=null)
transform.gameObject.rigidbody.constraints = RigidbodyConstraints.None;
}
}
}
This logs "im a child" for each child gameObject but does not remove the constraints on the rigid-bodies. Any idea what I'm doing wrong?
Comment
Answer by getyour411 · Mar 29, 2014 at 12:29 AM
Don't you need to use if(child.gameObject)... and child.gameObject.rigidbody...
Brilliant, I was so close, here is the functioning code, thanks getyour411.
void OnTriggerEnter(Collider col){
if(col.tag=="EnemyType1"){
foreach (Transform child in transform)
{
Debug.Log (child.rigidbody);
if (child.gameObject.rigidbody!=null)
child.gameObject.rigidbody.constraints = RigidbodyConstraints.None;
}
}
}