- Home /
Updating Array inside a List
Greetings,
I'm currently facing a problem regarding update values of several arrays inside a list. The basic setup is the following: a List containing 3 arrays each comprised of 16 float numbers. My problem is regarding the update of the each array, whenever I update one, it also affects ALL other arrays present in the list, something that I don't want to happen. The update of each array is made by calling the FindHomography function.
The code is as follows (for a matter of simplicity I omitted most of the code, however I can post more if needed):
public float[] matrix = new float[16];
public const int arraySize = 16;
public List<float[]> matrixList;
void Start () {
InitMatrixList();
}
void InitMatrixList(){
int numPlanes = manager.numExistPlanes;
matrixList = new List<float[]>();
for(int i = 0; i <= numPlanes; i++){
matrixList.Add(matrix);
Debug.Log ("Matrix added");
}
}
void LateUpdate () {
planeMatrixNum = manager.activePlaneNum;
matrix = matrixList[planeMatrixNum];
FindHomography(ref source, ref destination, ref matrix);
}
Also I don't really know if it's relevant since I have very little experience but these matrix values are used within a shader (not written by me).
Answer by robertbu · Feb 27, 2014 at 06:41 PM
Most things are by reference in C#. You are creating only one matrix and assigning that same matrix to all of your entries in your list. Change line 14 to:
matrixList.Add(new float[16]);
And you can get rid of the 'new float[16]' on line 1.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134920im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
List/Array Not Updating 2 Answers
A node in a childnode? 1 Answer
List is empty in Update 0 Answers
What do you think of this? List, Vector 3 and Update 1 Answer