# C# Multidimensional arrays

The problem is with the constructor of a custom Matrix4by4 class. It takes some vector arguments and sets the values[,] in the matrix. It gives a NullReferenceException when the code reaches the constructor

Edit: Got it to work ! been able to translate the mesh filter vertices

Did you declare the size of the array before trying to access it's elements?

Like,

```
public float[,] values = new float[4,4];
```

?

Otherwise the variable is null.

I know you have already solved your issue (since you forgot to initialize your "values" array). However implementing a class which has a two dimensional array to represent a 4 by 4 matrix seems to be a waste of resources. From the way you initialize your components it seems you aim for a 4 by 4 transformation matrix for a homogeneous 3d space.

Unity already has the Matrix4x4 struct. It provides you with various operators to do matrix-matrix multiplication as well as matrix vector multiplication. It also provides you with many helper methods / properties to get the determinant, inverse, transpose. As a struct it has no garbage memory overhead.

Thanks for the info! Will do. As of now I'm doing this as part of coursework to learn unity as well as math so i will try and implement my own operators and methods and everything even though its kind of tedious. I will remember if in the future I build something using matrices.

Ok, if it's just for learning purposes it might be fine ^^. If you have any issues with matrices in general I've once created a matrix crash course.

If you also want to implement a property / method to calculate the inverse of a given matrix i recommend something like the Gauss-Jordan elimination over Cramer's rule. Calculating the adjugate matrix / cofactor matrix for a matrix greater than 3x3 is not recommended as the complexity grows very fast. For a 4x4 matrix you need 280 float multiplications and 103 float additions.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.