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 PijusVose · Jul 13, 2017 at 10:11 AM · arraystilemapinitialization

Array not Initialized for some reason

The array tiles in the Field class is for some reason null. Here's the script:

 private void GenerateTiles(int k, int j) {
         fields[k, j].tiles = new Tile[tileMapSize, tileMapSize];
 
         Tile[,] fieldTiles = fields [k, j].tiles;
         for (int x = 0; x < 10; x++) {
             for (int y = 0; y < 10; y++) {    
                 GameObject newTile = BuildTile (1f, "Tile");
                 newTile.name += "_" + x + y;
                 newTile.transform.SetParent (fields[k, j].field);
                 newTile.transform.localPosition = new Vector3 (x * tileSize - (float)fieldSize / 2f + (float)tileSize / 2f, 0f, y * tileSize - (float)fieldSize / 2f + (float)tileSize / 2f);
 
                 newTile.GetComponent<MeshRenderer> ().material = materials [0];
 
                 fieldTiles [x, y] = new Tile (fields[k, j], newTile.transform.position, newTile.transform);
             }
         }
     }


 public class Field {
 
     public Transform fieldParent;
     public Transform field;
     public Transform emptyField;
     public Tile[,] tiles;
     public Vector2 fieldCoordinates;
 
     public Field(int arraySize, Vector2 coordinates, Transform parent) {
         tiles = new Tile[arraySize, arraySize];
         fieldCoordinates = coordinates;
         fieldParent = parent;
     }
 
     public Field(Transform obj, Vector2 coordinates, Transform parent) {
         fieldParent = parent;
         fieldCoordinates = coordinates;
         field = obj;
     }
 
     public Tile GetTile(int x, int y) {
         return tiles[x, y];
     }
 

Comment
Add comment · Show 1
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 ShadyProductions · Jul 13, 2017 at 10:25 AM 0
Share

what line do you get the error? also show us where you create your fields array

1 Reply

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

Answer by Bunny83 · Jul 13, 2017 at 11:20 AM

Are you sure that the array is null or maybe just an element is null? You use this to initialize the array:

 fields[k, j].tiles = new Tile[tileMapSize, tileMapSize];

but you use those for loops to initialize

 for (int x = 0; x < 10; x++) {
     for (int y = 0; y < 10; y++) {    

If tileMapSize is larger than "10" you will have null elements in your array. If tileMapSize is smaller than 10 the for loop will throw an index out of bounds exception. Since you seemingly get a null reference exception i guess tileMapSize is larger than 10 and you try to access an element with index larger than "9".

I guess you want to replace that magic number "10" with "tileMapSize" in those two for loops

Comment
Add comment · Show 2 · 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 PijusVose · Jul 13, 2017 at 01:18 PM 0
Share

tile$$anonymous$$apSize is 10. When I write fields[5, 5] for example, it works, but when I pass my field to a method, the field is null. For Example : public Field[,] fields

 void Start() {
     Some$$anonymous$$ethod(fields[5, 5]);
 }
 
 void Some$$anonymous$$ethod(Field f) {
      Tile[,] tiles = f.tiles;
 
      Debug.Log(tiles[0, 0].name) // this returns null;

      Debug.Log(fields[5, 5].tiles[0, 0].name) // this returns the tile name;
 }
avatar image Bunny83 PijusVose · Jul 13, 2017 at 01:53 PM 0
Share

Without further checks i can guarantee that your posted example will yield the same result in both cases. Either there is something else missing or you interpreting the results wrong.

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

69 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

Related Questions

Initialization of Classes Instantiated in an Array in the Inspector 1 Answer

Unity Tile Array vs Int Array Performance 0 Answers

C# array initialization 3 Answers

Initialising List array for use in a custom Editor 1 Answer

How to use arrays with custom inspector code? 1 Answer


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