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 $$anonymous$$atrix4x4 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 deter$$anonymous$$ant, 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 eli$$anonymous$$ation 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
Follow this Question
Related Questions
Fields not populated during OnValidate on Editor startup 2 Answers
Approach for filling 6x6 matrix with shapes 2 Answers
Forward Kinematics with Denavit Hartenberg 0 Answers
NavMeshAgent Warp error 0 Answers
Assigning to array give NullReferenceExecption!?!! 0 Answers