Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ForgeStudios · Sep 17, 2013 at 01:24 AM · arraymatrixclass object

Setting the size of an array inside a class object, not working

I keep getting a "Object reference not set to an instance of an object" error every time I try to define the size of an array, which is located in an array of class objects. Here is an simplified, detached example of what I am trying to accomplish:

 var matrixItem : mClass[];
 
 class mClass{
     obj : Vector2[];
 }
 
 function Awake(){
     matrixItem = new mClass[10];
     for(i = 0; i < matrixItem.length; i++){
         matrixItem[i].obj = new Vector2[64];
     }
 }

Can anyone help me determine what I am doing wrong? Thank you for any advice!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Benproductions1 · Sep 17, 2013 at 02:00 AM

Hello,

Your problem is not the creation of the built-in array. It would have been a more simple thing to solve had you posted the actual error (including line number) rather than only quote it.

When you create a new built-in array of non-struct objects, then each element is always going to be null. So when you create a new mClass[2] you will get something line [null, null]. A null value does not have a obj field and therefore throws an error.

When using built-in arrays, you need to initialize the elements so that they are not null:

 for (i = 0; i < array.length; i++) {
      array[i] = new mClass();
      array[i].obj = new Vector2[64];
 }

For a Vector2 array however, you do not need to do this because Vector2 is a structure not a class.

Hope this helps,
Benproductions1

Comment
Add comment · Show 3 · Share
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 ForgeStudios · Sep 17, 2013 at 11:24 AM 0
Share

Thank you for the input, but I still do not get an array of vectors in each of the array objects. This is what my script snippet currently looks like after using the suggestion you provided:

 for(i = 0; i < terrainSet.length; i++){
     terrainSet[i] = new array$$anonymous$$atrix();
     terrainSet[i].matObj = new Vector2[2048];
     for(j = 0; j < terrainSet[i].matObj.length; j++){ 
         terrainSet[i].matObj[j]=Vector2(sData.terrain.sObj[j +    ((i-1)*2048)].obj.pos.x, sData.terrain.sObj[j + ((i-1)*2048)].obj.pos.z);
         }
     }

The interesting thing is that the first object of the "matrix" is returning 2048 vector2 items but the second one isn't: http://puu.sh/4twD$$anonymous$$.png

avatar image Benproductions1 · Sep 17, 2013 at 12:11 PM 0
Share

do you get any errors? That code should work

avatar image ForgeStudios · Sep 17, 2013 at 12:16 PM 0
Share

It works! I'm sorry that was my fault:

 [j + ((i-1)*2048)]
 //should be
 [(i*2048) + j]

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

17 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

Related Questions

Recursive Tree Loop? How do i make the matrix-array? 3 Answers

As I compare 4 objects in an array? 2 Answers

IndexOutOfRangeException: Array index is out of range. 1 Answer

Bigger matrix? 3 Answers

How destroy a object and it's similars 4 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