- Home /
Question by
fantasyMe · Jul 26, 2011 at 09:52 AM ·
nullmultidimarray
c# 2D array error: need Help!
public void SetWorldMapOutline()
{
worldMap = new MapTileData[700, 700];
for (int y = 0; y < 700; y++)
{
axisY = size - y;
for (int x = 0; x < 700; x++)
{
worldMap[y, x].X = 0;
}
}
}
worldMap[y,x] is null. I do not understand why?
Comment
Best Answer
Answer by IainStanford · Jul 26, 2011 at 10:39 AM
Although you have instantiated an array of [700,700] MapTileData objects, you haven't instatiated the objects within that array, the following line
worldMap = new MapTileData[700,700]
makes an array for the objects, but doesn't create the actual objects within it. To fill the array you have to iterate over all the objects and do something along the lines of,
for(int i=0; i<700; i++){
for(int j=0; j<700; j++){
worldMap[i,j] = new MapTileData();
}
}
So what you could have at the end is...
public void SetWorldMapOutline()
{
worldMap = new MapTileData[700, 700];
for (int y = 0; y < 700; y++)
{
axisY = size - y;
for (int x = 0; x < 700; x++)
{
worldMap[y, x] = new MapTileData();
worldMap[y, x].X = 0;
}
}
}
or similar.
Your answer
Follow this Question
Related Questions
TextAsset / Resources.load return null 4 Answers
gameobject null 1 Answer
GetComponent() returns null 1 Answer
A script by that name already exists... so what? 5 Answers
Issues rotating around transform.up 2 Answers