- Home /
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.
Answer by Bunny83 · Jun 30, 2012 at 01:43 AM
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 ;)
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:
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 ^^
btw, the name variable has a nice side effect. Usually the array elements are called Element0, Element1 ... in the inspector. If the element is a serializable class, Unity will show the first string it founds in the class as name.
So do I create another script called WaypointPath, attach that to the gameObject and assign the points there, then access WaypointPath from the other script? I did that first and it worked but I had to make it a $$anonymous$$onobehaviour otherwise it gave me an error and then the name string being the element name didn't work.
Wait, scratch that I didn't need to make it a $$anonymous$$onobehaviour, but the array element names feature still doesn't work. The variable "name" is greyed out so I can't access it. Having that feature isn't essential but it is nice :D
exactly, it's just a "normal" class that is derived from System.Object by default. It also doesn't need to be placed in another file. It can be in the same file as you $$anonymous$$onoBehaviour script. Only $$anonymous$$onoBehaviour or ScriptableObject derived classes have to be in a file that is named like the class, otherwise you can't attach them as components.
grayed out? if it's a public field it should be editable like any other variable.
I will edit the answer and make the example a bit more complete, just in case ;)
Oh! Alright that answers my question! Thank you. Yeah no idea why it was greyed out, it was a public field but it isn't anymore.