So, what I need is to store every rotation of every children of a GameObject, in order to restore their original rotation as soon as I need it.
So what I’m doing here is I call a function which stores the rotations in a List of Vector3 (I’m using eulerAngles).
I did the following:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Example : MonoBehaviour {
List<Vector3> rotations = new List<Vector3> ();
void SetPositions () {
foreach (Transform t in GetComponentsInChildren <Transform> ())
rotations.Add (t.eulerAngles);
}
Then, I call that function inside Start ()
void Start () {
SetPositions ();
}
Then, every time I need to restore rotations, I call this:
void restore () {
for (int a = 0; a < GetComponentsInChildren <Transform> ().Length;)
GetComponentsInChildren <Transform>() [a] = rotations [a++];
}
}
The real issue being, each time I call that restore () function some child objects are not rotated correctly. Not only that, but an error shows up in the console stating that the index is out of range.
So I just change this piece:
GetComponentsInChildren <Transform>() [a] = rotations [a++];
To this:
GetComponentsInChildren <Transform>() [a]
= rotations [a++];
And the error highlites the “rotations [a++]” line.
Changing the for cycle to for (int a = 0; a < GetComponentsInChildren ().Length; a++)
and rotations [a++] to rotations [a] doesn’t seem to do anything.
I tried to Debug.Log (t.name+": "+t.eulerAngles); and each children name, along with the angles, shows up perfectly in the console.
I even tried changing List to List , but it didn’t work.
Also, when I tried that in previous projects, it turned out every element of that list updated each time the real Transform did, and I belive it is because the Transform class can’t be Instantiated.
So what am I doing wrong?