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 /
avatar image
0
Question by metaphysician · Feb 24, 2017 at 10:10 AM · scripting problemarrays

need help initializing custom multidimensional array

i would like to do a 3D array of a class rather than an integer. what i'd like to have is something like:

 public CellData[,,] constellationInstance = new CellData[8,8,8];  
  
 public class CellData
     {
         //define a Game Object to be associated with at Start
         public GameObject cellObject;
         //is the cell enabled? (true/false)
         public bool cellEnabled=false;
         //define an Intensity value (same value for all rows at a position)
         public int cellIntensity;
     }

but it seems the issue is that i can't seem to set values because nothing's been initialized in advance. checking the manual assignment examples on MSs site doesn't seem to be helpful. using for loops seems like the way to go like so:

     for (int i = 0; i < 8; ++i)
             for (int j = 0; j < 8; ++j)
                 for (int k = 0; k < 8; ++k){
                     constellationInstance[k,j,i].cellEnabled=false;
 
                     if(constellationInstance[k,j,i].cellIntensity == null)
                         constellationInstance[k,j,i].cellIntensity=100;
                     if(constellationInstance[k,j,i].cellObject == null)
                     {    
                         GameObject cellObj = GameObject.Find("Constellation/Layer_"+k+"/Row_"+rowNames[j]+"/pos"+i);
                          cellObj = cellObj.transform.GetChild(0).transform.gameObject;
                         constellationInstance[k,j,i].cellObject = cellObj;
                     }

}

running this i get an immediate NullRef, assumedly because i did not initialize the value properly. any clue how i can properly initialize this data?

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

2 Replies

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

Answer by jwulf · Feb 24, 2017 at 10:21 AM

When you initialize the array, you only reserve memory for 8*8*8 objects of class CellData, but you don't create the objects yet, so the array is initially filled with nulls if you will.

What you want to in you for loop is:

 for (int i = 0; i < 8; i++) {
     for (int j = 0; j < 8; j++) {
         for (int k = 0; k < 8; k++) {
             constellationInstance[k,j,i] = new CellData();
         }
     }
 }

Now your array is ready to use the way you want to. E.G. setting cellEnabled to false everywhere, cellIntensity to 100 everywhere or whatever you want.

Also note: You have this line in your code:

 constellationInstance[k,j,i].cellIntensity == null

And CellData.cellIntensity is an int - ints are non-nullable types, so can never be null.

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

Answer by metaphysician · Feb 24, 2017 at 10:32 AM

excellent - that did the trick. thanks much!

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

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

85 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

Related Questions

Rewind time (or problems with arrays) 1 Answer

Jagged Array returns null anywhere outside function that populates it 1 Answer

How to add an array to monobehaviour 1 Answer

How do i give Coordinates (x, y) to an Array 1 Answer

remove an item from an FSMarray using scripting 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