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.