Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DegaOne · Jan 21, 2018 at 09:21 PM · arraynullreferenceexceptionmatrixtranslation

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 alt text

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

alt text

untitled.png (19.0 kB)
untitled.png (77.5 kB)
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jan 21, 2018 at 09:37 PM 1
Share

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

avatar image DegaOne Cherno · Jan 21, 2018 at 11:37 PM 0
Share

I declared it as : public float [ , ] values;

avatar image Cherno DegaOne · Jan 21, 2018 at 11:39 PM 1
Share

Like,

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

?

Otherwise the variable is null.

Show more comments
avatar image Bunny83 · Jan 22, 2018 at 12:32 AM 1
Share

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.

avatar image DegaOne Bunny83 · Jan 22, 2018 at 01:03 AM 0
Share

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.

avatar image Bunny83 DegaOne · Jan 22, 2018 at 06:06 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

86 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges