Writing Jagged Arrays

For one of my scripts, the camera needs to follow a path with several different waypoints per path. So if there are 10 paths, then it needs to choose which path to take, then follow the waypoints of that path. So to hold all that information, I need to use jagged arrays. Although it takes up memory (as I’ve heard), it saves about a hundred lines of code per script (without the jagged array I would need to write an if statement for each of the paths and more paths are added as the game goes along so I need to use arrays to resize them). How do I write jagged arrays. I’ve tried this:

public Transform[][] steps = new Transform[10][];

I’m assuming the first set of brackets is the number of arrays in the overall jagged array. But with that code, the variable doesn’t show up in the Inspector. Is that how its supposed to be, or am I writing something wrong? If it’s intended to be like that, how can I make it visible in the Inspector? I’ve already tried [ExecuteInEditMode]. What am I doing wrong here? Thanks in advace.

Jagged arrays as well as multidimensional arrays can’t be serialized by Unity, so they can’t be saved and don’t show up in the inspector. However, there’s an easy workaround and in your case it’s even the better way :wink:

Just create a container class for a single path and then create an array of those:

// C#
// MyWaypointScript.cs
using UnityEngine;

[System.Serializable]
public class WaypointPath
{
    public string name;
    public Transform[] points;
}

public class MyWaypointScript : MonoBehaviour
{
    public WaypointPath[] paths;
}

That’s the result:

JaggedArray

Btw, you don’t need to create the array since if the variable is serializable, Unity creates it automatically.

edit sorry, forgot the [System.Serializable] attribute ^^