2D Transform Array, Tile coordinates and calling a script from it...?

hi there…

i tried to create a 2D tranform Array like this:

	public Transform[][] tilemap;

...

		tilemap = new Transform[320][];
		for(maprun_x=0; maprun_x<320; maprun_x++){
			tilemap[maprun_x] = new Transform[320];
		}

And i want to fill it with this:

		for(maprun_y=childholder.childCount-1; maprun_y>-1; maprun_y--){
			Array_index_x = (int)(childholder.GetChild(maprun_y).position.x/size.x);
			Array_index_y = (int)(childholder.GetChild(maprun_y).position.z/size.x);
			Debug.Log(Array_index_x + " | " + Array_index_y);
			//tilemap[Array_index_x][Array_index_y] = gameObject.GetComponent<BlockStats>();
			if(tilemap[Array_index_x][Array_index_y] != null){
				Debug.LogError("pos " + Array_index_x + "," + Array_index_y + " is not null");
			}
			else{
				tilemap[Array_index_x][Array_index_y] = childholder.GetChild(maprun_y);
			}
		}

each child is a single block. The blocks stay side by side to fill up a the map with tiles.

now i wanted to call a script from the block under the mouse position:

			mousepoint = Camera.main.ScreenPointToRay(Input.mousePosition);
			
			if (Physics.Raycast(mousepoint, out hit)) {
				if (hit.transform != null) {
			        mouse3dtile = new Vector2((int)(hit.transform.position.x/size.x), (int)(hit.transform.position.z/size.x));
					if((int)old_transform.x != (int)mouse3dtile.x){
						Debug.Log("Old_X: " + (int)old_transform.x + "| Old_Y:" + (int)old_transform.y + "| Old_Inhalt: " + tilemap[(int)old_transform.x][(int)old_transform.y]);
						tilemap[(int)old_transform.x][(int)old_transform.y].gameObject.GetComponent<BlockStats>().highlightoff();
						old_transform = new Vector2(mouse3dtile.x, mouse3dtile.y);
						Debug.Log("New_X: " + (int)old_transform.x + "| New_Y:" + (int)old_transform.y + "| New_Inhalt: " + tilemap[(int)old_transform.x][(int)old_transform.y]);
						tilemap[(int)old_transform.x][(int)old_transform.y].gameObject.GetComponent<BlockStats>().highlighton();
					}
					//highlight model, reset old_transform, set new old_transform
				}
			}

problem…
I get one NullReference Error after another… It seems like every Field of the Array is “null”…

BlockStats is the name of the C# Script, wich is inside each Block

Before this way i tried to let each block interact with the ray… but the update function of each block is in the block-count i use… too laggy (1-3 FPS). So i want to call a script inside a block, when the mouse is over it.

1st Question: Why is the Transform array null on every Field?
2nd Question: It happens sometimes that

is called… Means… the calculation of the array-field index gives an equal result as another block - why is this happening?

Why not use proper 2d array, instead of a jagged?
Transform[x,y] instead of Transform[×][y]
tileMap = new Transform[sizeX,sizeY]

also

mouse3dtile = new Vector2((int)(hit.transform.position.x/size.x), (int)(hit.transform.position.z/size.x));
doesnt look right to me, wont that give you some result between 0 and 1, which then immediatly casts to int?
When i work with tiles, i try to get them instanced exactly on the unit grid, so the tile index is just == its position
tilemap[0,0] is at position 0,0,0, tilemap[10,5] is at position 10,0,5, etc

the problem is, that the tiles are created earlier. The Transform array is created during the mission instruction cutscene (so the loading time isn’t affected that much). There was my idea:
Get the XYZ position of each child (tile), take the XZ and calculate the array index with it…

Edit:
switched to proper 2D Array… still the problems :confused:

Edit2:
i’ve still trouble with fixing this…

here’s a demoproject only including the maploader code that gives the error + model and scene…

i hope someone can help me here :frowning:

<< KotD.rar >>

See also: http://answers.unity3d.com/questions/438770/nullreferenceerror-tilemap-array-with-transforms-a.html

I tried now to create the map block by block, and put it directly into a 2D Array at instantiating…
The Map Loading time took around 8 Minutes for a 320x320 Map - wich is a bit too long.

Isn´t there a faster way to store the blocks into an array without minutes of loading time (i think 30 seconds Loading time is acceptable for such a map - It should stay below a minute in complete).