- Home /
C# declaring an array of transform arrays
I'm looking to make an array of array of transforms that can be accessed from the unity editor window, how would I go about doing that? I tried using jagged arrays but they cannot be accessed through the editor window. I'm trying to do this to organize and categorize my items for a procedural generation script.
Try this:
[Serializable]
public class TransformClass {
public Transform this;
public Transform[] transform;
}
public TransformClass[] transforms;
You can Serialize an array of a serializable struct containing another array. Should give you the array of arrays you want
Answer by lordlycastle · Mar 28, 2015 at 02:28 PM
If it’s just for use in Unity Editor, you could just create multiple public variable of arrays for different “category”, and they will show up as in categories. Otherwise you can create a Serializable
class which contains a array variable, as pointed out by @MakakWasTaken in the comment above. Although, I would recommend that you not use array in C# because they are immutable i.e. cannot change it’s value after initialisation. Try to use Lists they are more handy.
using System.Collections.Generic;
[Serializable]
public class Category {
public List<Transform> items = new List<Transform>();
}
public List<Category> categories = new List<Category>();
//To get object the format will be following
var someTransform = categories[2].items[1];
var otherTransform = categories[0].items.Find(someOtherTrasnform);
However, I would not recommend using this at all because this really isn’t that useful way to organise your data. I don’t know if you plan to use it, but if it's just for the Editor then I would recommend that you invest in an asset called Advanced Inspector it’ll help solve inspector display problems.
Hello, thank you for your answer, but I'm still having troubles with the serializable flag:
[Serializable]
Unity refuses to recognize it, is there a library I need to call to get it working? Here is an excerpt of my code as it stands
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[Serializable]
public class TransformArray{
public Transform[] arrayObject;
public TransformArray(Transform[] _ao){
arrayObject = _ao;
}
}
public class buildingObject : $$anonymous$$onoBehaviour {
public TransformArray[] longTransformArray;
//The rest of my code...
When you get this kind of issue and you do not know where the class/atrribute comes from, right click on it then Resolve, it will add it. In your case it is System.
As @lordlycastle said I think it might be better to use a List ins$$anonymous$$d so you can use .Add to add transforms at runtime, then if you need it as an array for a reference you can simply call
Transform[] someArray = longTransformArray.arrayObject.ToArray();
@fafase Thank you, that did it! I used the resolve command in Unity's monodevelope interface, for future reference of anyone who has the problem all it does is add this to the code: using System;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Set Current Array GameObject to Active 1 Answer
How to check for an empty array? 1 Answer
Deleting an Element from an Array. 1 Answer