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
1
Question by Ramun Flame · Nov 22, 2013 at 05:41 PM · textarrayslevelclassesinitialize

Accessing/Initializing array that's part of a class.

Hello,

I've avoided having to post a question so far, just finding my information by searching, but I've spent way too long trying to figure this out.

I'm making a grid based level, and I'm reading from a text file to populate my array. I made a class that only has one multidimensional array(but it doesn't work with regular arrays either). Then I make an array of this class, so I can store my levels in an array.

I keep getting this error:

"NullReferenceException: Object reference not set to an instance of an object"

Here is the class, declaration of variable, and function it is used.

 [System.Serializable]
 public class LevelsClass
 {
     public int[,] Tile;;
 
 }

 public class StageScript : MonoBehaviour {

 LevelsClass[] Levels;
 
     void CreateLevelArray()
     {
         Levels = new LevelsClass[1];
         Levels[0].Tile = new int[10, 10]; //ERROR HERE
         Levels[0].Tile[0,0] = 1;
 
 //Rest of this doesn't really matter, since the top is to test and it is getting an error.
 
         string l = TxtLevels.ToString();
         var le = l.Split('!');
         //Debug.Log(lv[0]);
 
         int i = 0;
         int j = 0;
         int h = 0;
 
         var lev = le[0].Split('\n');
         var leve = lev[0].Split(',');
 
         int[,] level = new int[0,0];
         Levels = new LevelsClass[le.Length];
         while (h < le.Length)
         {
             lev = le[h].Split('\n');
             Levels[h].Tile = new int[yMax - 1, xMax - 1];
             while(i < yMax - 2)
             {
                 leve = lev[i].Split(',');
                 while(j < xMax - 2)
                 {
                     
                     Levels[h].Tile[i,j] = int.Parse(leve[j]);
                 }
                 leve = null;
                 j = 0;
                 i++;
             }
             lev = null;
             i = 0;
             h++;
         }
     }
 }

I don't know if reading it into the array will work yet, but I can't even initialize the array from the class. Anyone know what I'm doing wrong.

Thank you so much in advance!

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
0
Best Answer

Answer by Fornoreason1000 · Nov 22, 2013 at 05:51 PM

  public int[,] Tile;;

this line confuses me on why you didn't get an error there. there should be only 1 semicolon

  Levels[0].Tile = new int[10, 10]; //ERROR HERE
        Levels[0].Tile[0,0] = 1;

there's is actually nothing wrong with the line itself, the compiler is telling you that Tile is null. because you didn't assign the LevelClass[0] (you assigned the array but no its values)

so try this

   Levels = new LevelClass[1];
   Levels[0] = new LevelsClass();  //need to be assigned
    Levels[0].Tile = new int[10, 10]; //ERROR HERE
        Levels[0].Tile[0,0] = 1;

its a good habit to initialize everything to avoid these errors, even when your not using those specif values at all. with ints, floats, doubles the compiler tends to initialize them behind the scenes giving them zero. array don't and neither do there values.

some reference on arrays

http://msdn.microsoft.com/en-us/library/vstudio/2yd9wwz4.aspx

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 Eric5h5 · Nov 22, 2013 at 05:57 PM 1
Share

You can have as many semicolons as you want, since they indicate the end of a statement, and empty statements are fine. This is perfectly valid code:

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Not very useful, but valid....

avatar image Fornoreason1000 · Nov 22, 2013 at 06:16 PM 0
Share

@Eric5h5 well that explains that

@Ramun Flame sorry made some typos in the Answer, what i meant to say was that Levels[0] was null becuase only the array itself was assigned

avatar image Ramun Flame · Nov 23, 2013 at 04:26 PM 0
Share

Thanks so much! I didn't realize you had to initialize each part of the array before assigning. Works great now.

Also, the double semicolon was just an typing/pasting error. It wasn't actually in my code. I was deleting a comment after pasting, and an extra semicolon must have survived. Thanks again!

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

18 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

Related Questions

Array of custom class objects, possible? 1 Answer

Call method on "Start" of a built-in component? 1 Answer

Need help with assigning game objects to variable 2 Answers

Creating 3D levels with text and XML files 2 Answers

Getting an error trying to assign a value to an array of classes element 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