• 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 TheLeadHound · Mar 03, 2019 at 06:26 PM · arrayseditorwindow

Elements of a 2D array are always null

I set up a tool a generate tiles for a level and store them in a 2D array. When I try to access them after I start the game, They always return null. Here is the code for the editor tool:

 using UnityEngine;
 using UnityEditor;
 using UnityEngine.UI;
 using System.Collections;
 
 public class TileGenerator : EditorWindow
 {
 
     static int y = 16;//size of the map
     static int x = 16;
 
     [MenuItem("Tile Board/Generate Tiles")]
     static void Begin()
     {
         TileGenerator selector = (TileGenerator)EditorWindow.GetWindow(typeof(TileGenerator));
         selector.Show();
     }
 
     void OnGUI()
     {
         y = EditorGUILayout.IntField("Height of map", y);
         x = EditorGUILayout.IntField("Width of map", x);
 
         if (GUILayout.Button("Generate Map"))
         {
             Create();
         }
     }
 
     static void Create()
     {
         Texture2D tilePNG = Resources.Load<Texture2D>("Sprites/Blank Tile");
         float Xdist = tilePNG.width/100.0f; //The x distance between collumns of tiles
         float Ydist = tilePNG.height/100.0f; //The y distance between rows of tiles
         Sprite tileImage = Sprite.Create(tilePNG, new Rect(0,0,tilePNG.width,tilePNG.height),new Vector2(.5f,.5f));//loads the default tile image so it does not need to be loaded everytime for each tile
 
         GameObject tileBlueprint = GameObject.Find("Tile Container");
         if (tileBlueprint != null)
         {
             GameObject.DestroyImmediate(tileBlueprint);
         }
         GameObject tileContainer = new GameObject("Tile Container");
         ZoneValues zoneInfo = tileContainer.AddComponent<ZoneValues>();
         zoneInfo.XSize = x;
         zoneInfo.YSize = y;
 
         for (int i = 0; i < x; i++)
         {
             for (int j = 0; j < y; j++)
             {
                 tileBlueprint = new GameObject("Tile (" + i + "," + j + ")");
                 tileBlueprint.transform.position = new Vector3(i * Xdist, j * Ydist, 1);//places the tile in position based on the coordinates
                 tileBlueprint.transform.tag = "Tile";
 
                 SpriteRenderer renderer = tileBlueprint.AddComponent<SpriteRenderer>();
                 renderer.sprite = tileImage;
                 tileBlueprint.transform.parent = tileContainer.transform;//puts Tile into easy to collapse container
                 renderer.sortingLayerName = "Tile";
 
                 TileValues thisTilesValues = tileBlueprint.AddComponent<TileValues>();
                 thisTilesValues.XPos = i;
                 thisTilesValues.YPos = j;
             }
         }
 
         //zoneInfo.StoreZone();
         zoneInfo.SetUpTiles();
         Debug.Log(zoneInfo.Tiles[0,0]);
     }
 }
 

This is the class where I store the array:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ZoneValues : MonoBehaviour
 {
     public int xSize;
     public int ySize;
     private GameObject[,] tiles;
 
     public void SetUpTiles()
     {
         tiles = new GameObject[xSize, ySize];
         for (int i = 0; i < xSize; i++)
             for (int j = 0; j < ySize; j++)
                 tiles[i, j] = GameObject.Find("Tile (" + i + "," + j + ")");
     }
 
     public GameObject[,] Tiles
     {
         get { return tiles; }
     }
 
     public int XSize
     {
         get
         {
             return xSize;
         }
         set
         {
             xSize = value;
         }
     }
 
     public int YSize
     {
         get
         {
             return ySize;
         }
         set
         {
             ySize = value;
         }
     }
 
 }
 

And when I call it with this line during run time I get a null error:

             SetCurrentTile(zoneValues.Tiles[currentX,currentY]);

This system was working before I tried to store the tiles in a 2D array and I searched for them in the game hierarchy, but it would be much more efficient to directly access the tiles from a 2D array rather than doing a search every time I want the character to move.

Comment
Add comment · Show 5
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 xxmariofer · Mar 03, 2019 at 08:34 PM 0
Share

where are you calling zonevalues? since zonevalues gets destroyed all the time you call the Create method and maybe you are not updating the zonevalues reference, and you are only finding the reference once at the start/awake.

avatar image TheLeadHound xxmariofer · Mar 03, 2019 at 08:58 PM 0
Share

This is called in a class called PartyController: private void Start() { zoneValues = GameObject.Find("Tile Container").GetComponent(); previousTile = zoneValues.Tiles[currentX, currentY]; currentTile = zoneValues.Tiles[currentX, currentY]; } I tested this by remaking part of it with a list ins$$anonymous$$d of a 2d array and the list does not come up null while the 2D array still does. In other words I could store these values in the list and read hem into a 2D array at start time, but that just seems so inefficient to me.

avatar image xxmariofer TheLeadHound · Mar 03, 2019 at 09:15 PM 1
Share

the setuptiles is being called before the partycontroller start gets called? there should not be much performance difference between accessing a the array or a list, and you could save some extra performance doing all the double for loops in just a single one. tiles[(i * XSize) + j];for accessing the list as if it was a bidimensional array

Show more comments
avatar image WarmedxMints · Mar 04, 2019 at 01:36 AM 0
Share

Unity cannot serialize properties and you have tiles encapsulated by Tiles.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

105 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 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

A list of all NPCs in Editorwindow - not working and I'm not sure why 2 Answers

Failed setting triangles in my mesh 1 Answer

Problem with List 0 Answers

Trying to populate an array with a GameObject 1 Answer

Cannot add Instaniated gameObjects to a GameObject[] using Javascript. Error: "NullReferenceException: Object reference not set to an instance of an object" 2 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