- Home /
Question by
theobbb · Dec 09, 2019 at 10:23 PM ·
gameobjectjointforeach
Reference Object Instance
Hi! I'm using a quaternion Awake state for some rigidbodies (BodyParts) and I can't find a way to wrap them in a cleaner way... (I have a lot more). I want to be able to reference this Awake state later. Thank You!
public GameObject[] BodyParts;
Quaternion ex1, ex2, ex3, ex4, ex5;
private void Awake()
{
// something like this
foreach (GameObject part in BodyParts)
part.GetComponent<ConfigurableJoint>().targetRotation = new Quaternion();
// instead of this
ex1 = BodyParts[1].GetComponent<ConfigurableJoint>().targetRotation;
ex2 = BodyParts[2].GetComponent<ConfigurableJoint>().targetRotation;
ex3 = BodyParts[3].GetComponent<ConfigurableJoint>().targetRotation;
ex4 = BodyParts[4].GetComponent<ConfigurableJoint>().targetRotation;
ex5 = BodyParts[5].GetComponent<ConfigurableJoint>().targetRotation;
}
Comment
Answer by voncarp · Dec 10, 2019 at 12:47 AM
Grab it all in one swoop. Add it to a list.
using System.Collections.Generic;
using UnityEngine;
public class HelpScript : MonoBehaviour
{
public GameObject[] BodyParts;
public Transform RootTransform;
List<Quaternion> ex;
ConfigurableJoint[] configurableJoints;
void Awake()
{
for (int i = 0; i < BodyParts.Length; i++)
{
configurableJoints = BodyParts[i].transform.GetComponentsInChildren<ConfigurableJoint>();
for (int z = 0; z < configurableJoints.Length; z++)
{
ex.Add(configurableJoints[z].targetRotation);
}
}
}
}
You can also grab them all from the root (top) transform.
void GrabFromRoot()
{
configurableJoints = RootTransform.GetComponentsInChildren<ConfigurableJoint>();
for (int z = 0; z < configurableJoints.Length; z++)
{
ex.Add(configurableJoints[z].targetRotation);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612215933im_/https://answers.unity.com/themes/thub/images/avi.jpg)