I am relativley new to scripting and am writing my first editor script where I am making a grid where you select certain cubes to make waypoint paths. I have no problem getting information from my monobehaviour scripts into my editor script but when I try to output an array built in the editor script to a Monobehaviour script and access its length I get a null reference exception. _mapArray is the script I am trying to to copy my Waypoints array to. It happens in the “Finalize Path” if statement. Any help would really be appreciated. I have been working on this for a few days I think I may need to use SerializeField or Scriptable Object but I’m not exactly sure what they do. I have looked at the documentation and through the forums but like I said I am new to scripting and don’t really understand it. Also the Debug.Log(_mapArray.VehicleWaypoints[i,j]) returns what I have selected it just doesn’t copy to my _mapArray script. Thanks in advance!
using UnityEngine;
using UnityEditor;
using System.Collections;
public class WayEditor : EditorWindow
{
private MapArray _mapArray;
private WaypointBool _waypointBool;
// SerializedObject target;
private int Length = 0;
private int Width = 0;
private float DistanceBetweenPointsX = 0.0f;
private float DistanceBetweenPointsZ = 0.0f;
private float _columnCount = 0.0f;
private float _rowCount = 0.0f;
private bool _once = false;
private bool _waypointsValid = false;
private bool _waypointParentValid = true;
private GameObject Wayparent;
public GameObject[,] Waypoints;
[MenuItem("WayEditor/WayEditor")]
public static void Init()
{
WayEditor window = (WayEditor)EditorWindow.GetWindow(typeof(WayEditor));
window.title = "WayEditor";
window.minSize = new Vector2(400 , 400);
window.maxSize = new Vector2(500 , 500);
}
private void OnGUI()
{
Length = EditorGUI.IntField(new Rect(5,200,300,20),"Length of Grid : ", Length);
Width = EditorGUI.IntField(new Rect(5,225,300,20),"Width of Grid : ", Width);
DistanceBetweenPointsX = EditorGUI.FloatField(new Rect(5,250,300,20),"Distance X : ", DistanceBetweenPointsX);
DistanceBetweenPointsZ = EditorGUI.FloatField(new Rect(5,275,300,20),"Distance Z : ", DistanceBetweenPointsZ);
if(GUILayout.Button("Add Waypoint Parent") && _waypointParentValid == true)
{
Wayparent = new GameObject("WayParent");
Wayparent.tag = "WayParent";
Wayparent.AddComponent<MapArray>();
// _mapArray = GameObject.FindGameObjectWithTag("WayParent").GetComponent<MapArray>();
// target = new SerializedObject(GameObject.FindGameObjectWithTag("WayParent"));
_once = false;
_waypointParentValid = false;
}
if(GUILayout.Button ("Create Grid") && Length > 1 && Width > 1 && _once == false)
{
Waypoints = new GameObject[Length,Width];
if(Wayparent != null)
{
Transform _wayParent = Wayparent.transform;
_rowCount = 0;
_columnCount =0;
for(int i = 0; i < Length; i++)
{
_rowCount = 0;
for(int j = 0; j< Width; j++)
{
Waypoints[i,j] = new GameObject("Waypoint " + i + " " + j);
Waypoints[i,j].transform.position = new Vector3(_wayParent.position.x + _rowCount, _wayParent.position.y,
_wayParent.position.z + _columnCount);
Waypoints[i,j].tag = "Point";
Waypoints[i,j].transform.parent = Wayparent.GetComponent<Transform>();
Waypoints[i,j].AddComponent<WaypointBool>();
_rowCount +=DistanceBetweenPointsX;
}
_columnCount += DistanceBetweenPointsZ;
}
_once = true;
_waypointsValid = true;
}
}
if(GUILayout.Button ("Finalize path") && _waypointsValid == true)
{
_mapArray = GameObject.FindGameObjectWithTag("WayParent").GetComponent<MapArray>();
_mapArray.VehicleWaypoints = new Vector3[Length,Width];
_mapArray.PedestrianWaypoints = new Vector3[Length,Width];
for(int i = 0; i < Length; i++)
{
for (int j = 0; j < Width; j++)
{
if(Waypoints[i,j].GetComponent<WaypointBool>().MyPath == WaypointBool.PathType.Vehicle)
{
_mapArray.VehicleWaypoints[i,j] = Waypoints[i,j].transform.position;
Debug.Log (_mapArray.VehicleWaypoints[i,j]);
}
if(Waypoints[i,j].GetComponent<WaypointBool>().MyPath == WaypointBool.PathType.Pedestrian)
{
_mapArray.PedestrianWaypoints[i,j] = Waypoints[i,j].transform.position;
Debug.Log (_mapArray.PedestrianWaypoints[i,j]);
}
}
}
}
if(GUILayout.Button ("Clear Array"))
{
DestroyImmediate(GameObject.FindGameObjectWithTag("Point"));
DestroyImmediate(GameObject.FindGameObjectWithTag("WayParent"));
_waypointParentValid = true;
_waypointsValid = false;
_once = false;
}
}
public class MapArray : MonoBehaviour
{
// Must have a VehiclePath Tagname and a PedestrianPath Tagname in tag manager
public Vector3[,] VehicleWaypoints;
public Vector3[,] PedestrianWaypoints;
// Use this for initialization
// Update is called once per frame
void Update ()
{
}
}