Hi I’m making an editor script for a tank NPC. You hold the mouse over the ground and press the g key to add a waypoint (a vector3) to the the tank’s waypoints array. The tank will loop through these waypoints to patrol a route in game. The system works fine in the editor; the waypoints are added to the tank’s waypoint array, but when the game begins the array is wiped and the tank no longer has any waypoints. If I type into the waypoints GUI area on the Tank_AI script box (the script which runs the tank) and type in waypoints myself, they survive into the game. Only the waypoints generated by the editor script do not. Does anyone have a solution?
The editor script is below:
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(Tank_AI))]
public class NPC_RouteDebug : Editor
{
public Tank_AI script;
void OnEnable()
{
script = (Tank_AI) target;
}
void OnSceneGUI()
{
Handles.color = Color.blue;
Handles.Label( script.transform.position + Vector3.up * 4,
script.transform.position.ToString() );
if ( script.waypoints != null )
{
for ( int i = 0; i < script.waypoints.Length; i++ )
{
Handles.Label( script.waypoints[i], i.ToString() );
}
}
RaycastHit hit;
Ray mouseRay = HandleUtility.GUIPointToWorldRay( Event.current.mousePosition );
if ( Physics.Raycast( mouseRay, out hit, Mathf.Infinity ) )
{
Handles.Label( hit.point, "Mouse" );
}
if ( Event.current.type == EventType.KeyUp Event.current.keyCode == KeyCode.G )
{
Vector3[] temp = script.waypoints;
script.waypoints = new Vector3[temp.Length + 1];
for ( int i = 0; i < temp.Length; i++ )
{
script.waypoints[i] = temp[i];
}
script.waypoints[script.waypoints.Length - 1] = hit.point;
}
if ( Event.current.type == EventType.KeyUp Event.current.keyCode == KeyCode.H )
{
script.waypoints = new Vector3[0];
}
}
}