- Home /
Question by
tonybera301 · Apr 15, 2021 at 10:27 AM ·
rigidbodyarray of gameobjectsmassprint
print the mass values of rigidbody attached with the gameobjects assigned to the array variable
i want a script , that should have the array variable to get the multiple gameobject as a elements and print
the mass values of rigidbody attached with the gameobjects assigned to the array variable. so far I am able to get the mass value of the child objects how do I combine it with an array system.
public void checker()
{
int children = this.transform.childCount;
for (int i = 1; i < children; ++i)
{
Rigidbody Rbd;
Rbd = this.transform.GetChild(i).gameObject.GetComponent<Rigidbody>();
float mass = Rbd.mass;
Debug.Log("mass" + mass);
}
}
Comment
Best Answer
Answer by Hellium · Apr 15, 2021 at 10:44 AM
Rigidbody[] childRigidbodies;
public void checker()
{
childRigidbodies = GetComponentsInChildren<Rigidbody>();
for (int i = 0; i < childRigidbodies.Length; ++i)
{
Debug.Log("Mass of child #" + i + " = " + childRigidbodies[i].mass);
}
}
If you only want the masses
float[] childMasses;
public void checker()
{
childMasses = System.Array.ConvertAll(GetComponentsInChildren<Rigidbody>(), rb => rb.mass);
for (int i = 0; i < childRigidbodies.Length; ++i)
{
Debug.Log("Mass of child #" + i + " = " + childMasses[i]);
}
}