- Home /
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.
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.
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.
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
Unity cannot serialize properties and you have tiles encapsulated by Tiles.
Your answer
Follow this Question
Related Questions
A list of all NPCs in Editorwindow - not working and I'm not sure why 2 Answers
Issues with string array taken from a .txt file 2 Answers
For loop not running correctly inside start () 1 Answer
Using arrays and for loop with GetComponent error 1 Answer
How to select a texture in an inventory? 0 Answers