x


Arrays not working in editor script.

I run my editor script and it works fine other than an issue that only happens half of the time when it accesses my [,] array. here's my code:

class MapCreator : EditorWindow {

int columns = 0;
int prevColumns = 100;
int rows = 0;
int prevRows = 100;
int tileCount = 0;
int[,] areaMap;
bool[,] passableMap;
GameObject[] prefabs;
int prevTiles = 1000;
float tileSize = 2;
Vector2 scrollPos;

[MenuItem("Window/MapMaker")]
static void Init(){
    MapCreator window = EditorWindow.GetWindow<MapCreator>("MapCreator");
}

void OnGUI(){
    EditorGUILayout.BeginVertical();
    scrollPos =    EditorGUILayout.BeginScrollView(scrollPos);
    columns = EditorGUILayout.IntField("Columns: ",columns);
    rows = EditorGUILayout.IntField("Rows: ",rows);
    if(rows != prevRows || columns != prevColumns)
    {
       areaMap = new int[columns,rows];
       int length = areaMap.Length;
       passableMap = new bool[columns,rows];
       prevRows = rows;
       prevColumns = columns;
    }
    EditorGUILayout.LabelField("Tiles");
    tileCount = EditorGUILayout.IntField("TileCount: ",tileCount);
    if(tileCount != prevTiles)
    {
       prefabs = new GameObject[tileCount];
       prevTiles = tileCount;
    }
    for(int t = 0;t<tileCount;t++)
    {
       Object prefab = prefabs[t] as Object;
       prefabs[t] = EditorGUILayout.ObjectField(t.ToString(),prefab,typeof(GameObject),false) as GameObject;
    }
    tileSize = EditorGUILayout.FloatField("Tile Size:",tileSize);
    EditorGUILayout.LabelField("Main Map");
    for(int c = 0;c<columns;c++)
    {
       EditorGUILayout.BeginHorizontal();
       for(int r = 0;r<rows;r++)
       {
         areaMap[c,r] = EditorGUILayout.IntField(areaMap[c,r],GUILayout.MaxWidth(20));
       }
       EditorGUILayout.EndHorizontal();
    }
    if(GUILayout.Button("Make Map"))
    {
       MakeMap();
    }
    EditorGUILayout.EndScrollView();
    EditorGUILayout.EndVertical();
}

void MakeMap(){
    bool rowsEven = true;
    bool columnsEven = true;
    Quaternion zeroRotation = new Quaternion(0,0,0,0);
    zeroRotation.eulerAngles = new Vector3(0,0,0);
    //Determine if the number of rows is even
    try
    {
       int n = rows/2;
    }
    catch
    {
       rowsEven = false;
    }
    //Determine if number of columns is even
    try
    {
       int n = columns/2;
    }
    catch
    {
       columnsEven = false;
    }
    float gridX;
    float gridY;
    if(rowsEven)
    {
       gridX = -rows/2*tileSize;
    }
    else
    {
       gridX = -rows/2*tileSize+tileSize/2;  
    }
    if(columnsEven)
    {
       gridY = -columns/2*tileSize;
    }
    else
    {
       gridY = -columns/2*tileSize+tileSize/2;
    }
    for(int c = 0;c<columns;c++)
    {
       for(int r = 0;r<rows;r++)
       {
         int tile = areaMap[c,r];
         switch(tile)
         {
         case 0:  
         {
          break;  
         }
         default:
         {
          if(tile>tileCount-1)
          {
              break; 
          }
          Instantiate(prefabs[tile-1],new Vector3(gridX,0,gridY),zeroRotation);   
          break;  
         }
         }
         gridX += tileSize;
       }
       gridY+=tileSize;
    }
}
}

Here's the error: NullReferenceException: Object reference not set to an instance of an object MapCreator.OnGUI () (at Assets/Editor/MapCreator.cs:56) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)

I am using C#. The cases in it which it is half working is when I call my make Map method. Any Ideas or fixes would be greatly appreciated.

more ▼

asked Jun 09 '12 at 06:07 PM

darklad gravatar image

darklad
44 1 3 5

Don't think your try catches are doing much in working out the even rows and columns - doesn't sound exceptional to me to divide a number by 2.

Perhaps you should be doing:

  rowsEven = rows % 2 == 0;
Jun 09 '12 at 06:10 PM whydoidoit

Which one of the lines you pasted in is line 56 in your script? It looks like you might have omitted a few lines, so it's hard to figure out exactly which line is throwing the exception.

Jun 09 '12 at 06:22 PM rutter

Thanks whydoidoit for that tip. I knew there was a easier way to do it but I forgot what it was. By the way what does the % sign do again.

Jun 09 '12 at 06:36 PM darklad

Oh sorry just saw this! % is modulus - the remainder when dividing by the number. so anything %2 will be 0 for even and 1 for odd

Jun 14 '12 at 11:10 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I figured it out. changing the variables to:

int[,] areaMap = new int[0,0];
bool[,] passableMap = new bool[0,0];

fixes the problem.

more ▼

answered Jun 09 '12 at 06:33 PM

darklad gravatar image

darklad
44 1 3 5

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x169

asked: Jun 09 '12 at 06:07 PM

Seen: 364 times

Last Updated: Jun 14 '12 at 11:10 PM