- Home /
Question by
GoGamingJoe · Apr 27, 2014 at 09:04 AM ·
rigidbodygetcomponentinchildren
Is GetComponentsInChildren What I'm Looking For
I'm trying to turn kinematic off so that some GameObjects will fall but I don't know what function I'm looking for GetComponentInChildren just gets it for the first one?
public bool IwantTheKamaticOff = false;
Rigidbody RidgeBod;
List <GameObject> Bricks = new List <GameObject>();
// Use this for initialization
void Start () {
RidgeBod = gameObject.GetComponentInChildren<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if (IwantTheKamaticOff == true) {
RidgeBod.isKinematic = false;
}
}
}
Thanks
Comment
That depends on whether your rigidbodies are children of the object with that script.
Answer by fafase · Apr 28, 2014 at 06:58 AM
More likely looking for GetComponent() but I would try to speed up the process a little:
Rigidbody rigArray;
void Start()
{
GameObject [] objs = GameObject.FindGameObjectsWithTag("RigObject");
List<Rigidbody> list = new List<Rigidbody>();
for(int i = 0; i < objs.Length; i++){
Rigidbody rig = objs[i].GetComponent<Rigidbody>();
rigList.Add(rig);
}
rigArray = list.ToArray();
}
void SetKinematic(bool value)
{
for(int i = 0; i < rigArray.Length; i++ )rigArray[i].isKinematic = value;
}
This way you are only looking for the object once in the start then you have a list of Rigidbody so you can skip the gameObject level and access directly the rigidbody.
Now if you were not to remove any of those objects you would use a list instead but the process is the same. But you also need to take care of the removal to avoid null references.