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?