Initializing array syntax/logic problem?

Hi,

Through the inspector I can set the size of the number of waypoints per path. How can I initialize it through code. I am stuck on how get the AddWaypoints() to change the element size of the waypoint array per path.

Ultimately I am trying to make an editor script that allows me to click in the scene view and generate way points that will be fed into the way points array of each path. This is triggered by a button in the inspector. And it happens with play mode OFF.

Any help is appreciated. These are the 2 classes:

using System;
using UnityEngine;

[Serializable]
public class PatrolPath
{
    public Transform[] waypoints;
}

The other 1 is:

using UnityEngine;

public class UnitSpawner : MonoBehaviour
{
    public PatrolPath[] paths;

    private int _pathCount = 0;
    private int _waypointCount = 0;

    public void CreateNewPath()
    {
        _pathCount++;

        // Generate a new path object
        paths = new PatrolPath[_pathCount];
    }

    public void AddWaypoints()
    {
        _waypointCount++;

        // Waypoint array is size 0 right now
        // Increase array size somehow???
        new paths[_pathCount].wayPoints[_waypointCount];
    }
}
if (paths == null) paths = new paths[_pathCount];

// Index is the path index that you want to modify. Pass this to the method instead of modifying last path
// Also, check if it's in bounds of paths' length.
Transform[] prevArray = paths[index].wayPoints;
Transform[] newArray = new Transform[_waypointCount];

// Don't forget to copy the prevArray to the new waypoints
if (prevArray != null && prevArray.Length > 0) {
    // Length is the previous count of elements to copy, so minus one if you've incremented it
    Array.Copy(prevArray, 0, newArray, 0, _waypointCount - 1);
}

paths.wayPoints = newArray;

Should be something like this.

Note that creating arrays constantly will allocate garbage and sink your performance.
Just use Lists. There’s not much of performance gain of arrays over the lists.

// In the data
List<Tranform> waypoints;

// Then do the add. Just don't forget to null check / initialize the waypoints list.
waypoints.Add(*your_point*);

I will try that out. Thanks.