private void OnEnable()
    {
        network = (RoadNetwork)target;

        // If this is the first time selecting a road, the global settings will not have been set yet so call ResetGlobals
        if (!areGlobalsSet)
        {
            ResetGlobals();
        }

        if (network.roads == null || network.roads.Count == 0)
        {
            network.InitialiseRoadNetwork(defaultMaterial);
        }

        selectedRoad = network.ActiveRoad;

        // Disables the default transform/rotation/other handle when first selecting the road network
        previousTool  = Tools.current;
        Tools.current = Tool.None;

        // Ensures the mesh is regenerated when the developer undos or redos
        Undo.undoRedoPerformed += GenerateMesh;

        network.OnRoadSelected += SelectRoad;
    }
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        EditorGUILayout.BeginHorizontal();
        // Delete the road
        if (selectedRoad == null)
        {
            GUI.enabled = false;
        }
        if (GUILayout.Button("Delete Road"))
        {
            DeleteRoad();
        }
        GUI.enabled = true;

        // Reset the entire road network
        if (GUILayout.Button("Reset Road Network"))
        {
            Undo.RecordObject(network, "Reset road network");
            network.InitialiseRoadNetwork(defaultMaterial);
            selectedRoad = network.roads[0];
            GenerateMesh();
            SceneView.RepaintAll();
        }
        EditorGUILayout.EndHorizontal();

        if (selectedRoad != null)
        {
            // Toggle whether or not the road loops
            bool isRingRoad = GUILayout.Toggle(selectedRoad.IsRingRoad, "Toggle Ring Road");
            if (selectedRoad.IsRingRoad != isRingRoad)
            {
                Undo.RecordObject(selectedRoad, "Toggle Ring Road");
                selectedRoad.IsRingRoad = isRingRoad;
                GenerateMesh();
            }

            // Road speed limit
            selectedRoad.SpeedLimit = EditorGUILayout.FloatField("Maximum speed limit", selectedRoad.SpeedLimit);

            // Overall road width
            float previousWidth = selectedRoad.RoadWidth;
            selectedRoad.RoadWidth = EditorGUILayout.FloatField("Road width", selectedRoad.RoadWidth);
            if (previousWidth != selectedRoad.RoadWidth)
            {
                GenerateMesh();
            }

            // Texture tiling value, sets the road texture to tile with a consistent length regardless of the road size
            float previousTiling = selectedRoad.TextureTiling;
            selectedRoad.TextureTiling = EditorGUILayout.Slider("Texture tiling", selectedRoad.TextureTiling, 0.01f, 1f);
            if (previousTiling != selectedRoad.TextureTiling)
            {
                GenerateMesh();
            }

            // Texture tiling value, sets the road texture to tile with a consistent length regardless of the road size
            Material previousMaterial = selectedRoad.Material;
            selectedRoad.Material = (Material)EditorGUILayout.ObjectField("Material", selectedRoad.Material, typeof(Material), false);
            if (previousMaterial != selectedRoad.Material)
            {
                selectedRoad.UpdateMaterial(selectedRoad.Material);
                GenerateMesh();
            }

            nodeEditingFoldout = EditorGUILayout.Foldout(nodeEditingFoldout, "Edit Node");

            // Allow the selected node to be edited
            if (nodeEditingFoldout)
            {
                bool doesNodeExist = false;
                try
                {
                    Vector3 node = selectedRoad[selectedNode];
                    doesNodeExist = true;
                }
                catch (System.ArgumentOutOfRangeException)
                {
                    GUILayout.Label("No node selected.");
                }
                if (doesNodeExist)
                {
                    // Move node field
                    Vector3 newPosition = EditorGUILayout.Vector3Field("Node Position", selectedRoad[selectedNode]);
                    if (selectedRoad[selectedNode] != newPosition)
                    {
                        Undo.RecordObject(selectedRoad, "Move Node");
                        selectedRoad.MoveNode(selectedNode, newPosition);
                        GenerateMesh();
                    }

                    // Insert button creates a new node between the selected node and the next one
                    if (selectedNode % 3 != 0)
                    {
                        GUI.enabled = false;
                    }
                    GUILayout.BeginHorizontal();
                    if (GUILayout.Button("Insert node"))
                    {
                        Undo.RecordObject(selectedRoad, "Insert road section");
                        selectedRoad.SeperateRoadSection(selectedNode);
                        selectedNode += 3;
                        GenerateMesh();
                    }
                    // Delete node button, only enabled if the selected node is an anchor node and there is more than 2 anchor nodes
                    if (selectedRoad.NodeCount < 7)
                    {
                        GUI.enabled = false;
                    }
                    if (GUILayout.Button("Delete Node"))
                    {
                        Undo.RecordObject(selectedRoad, "Remove Road Section");
                        selectedRoad.RemoveRoadSection(selectedNode);
                        GenerateMesh();
                    }
                    GUI.enabled = true;
                    GUILayout.EndHorizontal();

                    // Additional node information
                    EditorGUILayout.LabelField("Node Index: ", selectedNode.ToString());
                    EditorGUILayout.LabelField("Node Type: ", selectedNode % 3 == 0 ? "Anchor" : "Control");
                }
            }

            showEquidistantPointsFoldout = EditorGUILayout.Foldout(showEquidistantPointsFoldout, "Equidistant Point Settings");

            float previousEPD = selectedRoad.equidistantPointDistance;
            if (showEquidistantPointsFoldout)
            {
                selectedRoad.equidistantPointDistance = EditorGUILayout.Slider("Distance between points", selectedRoad.equidistantPointDistance, 0.05f, 4f);

                // Equidistant point information
                EditorGUILayout.LabelField("Number of points: ", selectedRoad.equidistantPoints.Length.ToString());
            }

            // Manually generate mesh
            if (GUILayout.Button("Generate Road Mesh") || previousEPD != selectedRoad.equidistantPointDistance)
            {
                GenerateMesh();
            }

            // Mesh data
            if (selectedRoad.equidistantPoints != null)
            {
                GUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Tris: " + 6 * selectedRoad.equidistantPoints.Length);
                EditorGUILayout.LabelField("Verts: " + 2 * selectedRoad.equidistantPoints.Length);
                GUILayout.EndHorizontal();
            }
        }

        globalSettingsFoldout = EditorGUILayout.Foldout(globalSettingsFoldout, "Global Settings");

        if (globalSettingsFoldout)
        {
            anchorNodeSize       = Mathf.Max(0, EditorGUILayout.FloatField("Anchor node size", anchorNodeSize));
            controlNodeSize      = Mathf.Max(0, EditorGUILayout.FloatField("Control node size", controlNodeSize));
            equidistantPointSize = Mathf.Max(0, EditorGUILayout.FloatField("Equidistant point size", equidistantPointSize));

            anchorNodeColour        = EditorGUILayout.ColorField("Anchor node colour", anchorNodeColour);
            controlNodeColour       = EditorGUILayout.ColorField("Control node colour", controlNodeColour);
            selectedNodeColour      = EditorGUILayout.ColorField("Selected node colour", selectedNodeColour);
            equidistantPointColour  = EditorGUILayout.ColorField("Equidistant point colour", equidistantPointColour);
            controlConnectionColour = EditorGUILayout.ColorField("Anchor-to-control line colour", controlConnectionColour);
            roadPathColour          = EditorGUILayout.ColorField("Road path colour", roadPathColour);
            vehiclePathColour       = EditorGUILayout.ColorField("Vehicle path colour", vehiclePathColour);

            defaultMaterial = (Material)EditorGUILayout.ObjectField("Default road material", defaultMaterial, typeof(Material), true);

            if (GUILayout.Button("Reset Global Settings"))
            {
                ResetGlobals();
            }
        }

        if (GUI.changed)
        {
            EditorUtility.SetDirty(network);
            if (selectedRoad != null)
            {
                Undo.RecordObject(selectedRoad, "Road changes");
            }
            else
            {
                Undo.RecordObject(network, "Road network changes");
            }
            GenerateMesh();
        }
    }