- Home /
List of Transforms updating all items when adding variable.
I'm attempting to create a list of Transforms filled from a variable that changes every FixedUpdate:
IEnumerator FillList(){
for (int i = 0; i < listItems; i++) {
transformList.Add(newTransform);
yield return new WaitForFixedUpdate();
}
}
The variable "newTransform" is changing every FixedUpdate, but when I fill the list it fills every item on the list with the same value, instead of adding the new updated value and keeping the old ones.
Any ideas on how to add a new value every loop?
I've also tried
transformList.Insert(i, newTransform);
I've tried both using a struct and a separate class to do something like:
transformList.Add(new TranPositions(newTransform));
Yet I keep getting the same results. Any help would be greatly appreciated! Thank you!
The list will surely get the object newTransform holds at the time of calling Add(). So my guess is the problem isn't in the shown code but in how and where newTransform is assigned values to or such.
Answer by · Jul 26, 2017 at 08:41 PM
From what I can tell, you are saving a transform reference. When you save a transform, you don't save the values of the transform, you save a reference for that transform. Try saving only the information that you need (ex: Vector 3 transform.position, Quaternion transform.rotation, Vector 3 transform.scale).
A TRUE HERO. This is the kind of information I have a hard time finding! Thank you so much for the help. When I chose a specific element (in this case newTransform.rotation) everything worked as it should.
Your answer
Follow this Question
Related Questions
Affect every object in array. 1 Answer
Copy values between two classes in two lists. 1 Answer
A node in a childnode? 1 Answer
merge sort crash Unity 2 Answers