Storing tag strings in an array, from an already existing array of gameobjects with different tags.

I don’t often ask any questions here. I tend to read documents and troubleshoot my own problems and figure them out, but this one is stumping me hard.

I have a 3D stageBuilder/mapMaker scene set up and it works by having an array of empty objects(We’ll call them EMs for short), all parented under one gameobject. There’s no instantiation in progress, but manipulation towards the empty gameobjects by giving them sharedMesh’s, collision, layers, and tags.

My problem is that I want to save all of my EMs and their attributes on a JSON.txt file, so that if someone were to make a custom stage/map, I’d be able to read it into the game and load it.

Is it possible to get a string array pulled from an array of gameobjects with different tags?
All of the EMs won’t be used, so some of them will be called “empty”, while some are used and called “ground”. I want to try and get their respective elements read, and then with their elements lined up, also fill in an empty string array I made. I hope I wasn’t too confusing.
I’m not even sure JSON allows this kind if mock-up.

My other solutions, that worked, was to create EVERY int/string myself calling them “tag0”,“tag1”…“tag100”. But I’d also have to do this for their position x, y and z, and their layers and so on. I want to cheat it essentially.

My code’s a little messy but this is my save/load system.

public GameObject[] EmptyObj;
public GameObject MeshCalc;

public string[] tagData;
public int index;
public int rawIndex;

int layerData0;
int layerData1;

int meshData0;
int meshData1;

Vector3 positionData;
Quaternion rotationData;

void Start()
    index = EmptyObj.Length;
    rawIndex = tagData.Length;

public void Save()
    JSONObject Empty = new JSONObject();
    //Save Tags
    tagData[rawIndex] = EmptyObj[index].GetComponent<EmptyObjectMeshCalc>().tagName;
    Empty.Add("tag" + EmptyObj[index], tagData[index]);
    //Save Layer
    layerData0 = EmptyObj[0].layer;
    layerData1 = EmptyObj[1].layer;
    Empty.Add("layer0", layerData0);
    Empty.Add("layer1", layerData1);
    //Save Mesh
    meshData0 = EmptyObj[0].GetComponent<EmptyObjectMeshCalc>().i;
    meshData1 = EmptyObj[1].GetComponent<EmptyObjectMeshCalc>().i;
    Empty.Add("meshData0", meshData0);
    Empty.Add("meshData1", meshData1);
    //Save Position
    JSONArray position0 = new JSONArray();
    Empty.Add("position0", position0);
    JSONArray position1 = new JSONArray();
    Empty.Add("position1", position1);
    //Save Rotation
    JSONArray rotation0 = new JSONArray();
    Empty.Add("rotation0", rotation0);
    JSONArray rotation1 = new JSONArray();
    Empty.Add("rotation1", rotation1);


    string path = Application.persistentDataPath + "/MapData.json";
    File.WriteAllText(path, Empty.ToString());

public void Load()
    string path = Application.persistentDataPath + "/MapData.json";
    string jsonFile = File.ReadAllText(path);
    JSONObject emptyJson = (JSONObject)JSON.Parse(jsonFile);

    //SET TAGS
    tagData[index] = emptyJson["tag"];

    layerData0 = emptyJson["layer0"];
    layerData1 = emptyJson["layer1"];
    EmptyObj[0].layer = layerData0;
    EmptyObj[1].layer = layerData1;

    //SET MESH
    meshData0 = emptyJson["meshData0"];
    meshData1 = emptyJson["meshData1"];
    EmptyObj[0].GetComponent<EmptyObjectMeshCalc>().i = meshData0;
    EmptyObj[1].GetComponent<EmptyObjectMeshCalc>().i = meshData1;
    EmptyObj[0].GetComponent<MeshFilter>().sharedMesh = MeshCalc.GetComponent<CreateObjects>().Platforms[meshData0].GetComponent<MeshFilter>().sharedMesh;
    EmptyObj[0].AddComponent<MeshCollider>().sharedMesh = MeshCalc.GetComponent<CreateObjects>().Platforms[meshData0].GetComponent<MeshFilter>().sharedMesh;
    EmptyObj[1].GetComponent<MeshFilter>().sharedMesh = MeshCalc.GetComponent<CreateObjects>().Platforms[meshData1].GetComponent<MeshFilter>().sharedMesh;
    EmptyObj[1].AddComponent<MeshCollider>().sharedMesh = MeshCalc.GetComponent<CreateObjects>().Platforms[meshData1].GetComponent<MeshFilter>().sharedMesh;

    EmptyObj[0].transform.position = new Vector3(emptyJson["position0"].AsArray[0], emptyJson["position0"].AsArray[1], emptyJson["position0"].AsArray[2]);
    EmptyObj[1].transform.position = new Vector3(emptyJson["position1"].AsArray[0], emptyJson["position1"].AsArray[1], emptyJson["position1"].AsArray[2]);

    EmptyObj[0].transform.eulerAngles = new Vector3(emptyJson["rotation0"].AsArray[0], emptyJson["rotation0"].AsArray[1], emptyJson["rotation0"].AsArray[2]);
    EmptyObj[1].transform.eulerAngles = new Vector3(emptyJson["rotation1"].AsArray[0], emptyJson["rotation1"].AsArray[1], emptyJson["rotation1"].AsArray[2]);


And every EM has this script attached:

public class EmptyObjectMeshCalc : MonoBehaviour
    public int i;
    public string tagName;

If you need anything else, I’d be happy to provide. Thanks again in advance!

Create an entity for your pattern. In your case that would be like :

using System;
 public class EmptyObjData {
      public string tag;
      public List<float> position;
      public List<float> rotation; //as euler angles
      public float layer;

Then on Save method get all your gameobjects in an array and do this loop

public void Save() {
      GameObject[] arr = [your empty gameobjects];
       var wrapper = new EmptyObjDataWrapper();
       var dataList = new List<EmptyObjData>();
       for (int i = 0; i < arr.Length; i++) {
           var t = arr*.transform;*

var d = new EmptyObjData {
tag = arr*.GetComponent().tagName,*
position = new List() { t.position.x, t.position.y, t.position.z },
rotation = new List() { t.rotation.eulerAngles.x, t.rotation.eulerAngles.y, t.rotation.eulerAngles.z },
layer = arr*.layer*
wrapper.emptyObjDataArray = dataList.ToArray();
string jsonFile = JsonUtility.ToJson(wrapper);
//Then save json as text with the same (MapData.json in your case)
When you deserialize this saved data you will get wrapper class. So set all values back again in a for loop
public void Load() {
string path = Application.persistentDataPath + “/MapData.json”;
string jsonFile = File.ReadAllText(path);
//JSONObject emptyJson = (JSONObject)JSON.Parse(jsonFile);

EmptyObjDataWrapper wrapper = JsonUtility.FromJson(jsonFile);
GameObject[] arr = [your empty gameobjects];

for (int i = 0; i < wrapper.emptyObjDataArray.Count; i++) {
arr_.tag = wrapper.emptyObjDataArray*.tag;
arr.transform.position = new Vector3(wrapper.emptyObjDataArray.position[0], wrapper.emptyObjDataArray.position[1], wrapper.emptyObjDataArray.position[2]);
wrapper.emptyObjDataArray.transform.rotation = Quaternion.Euler(new Vector3(wrapper.emptyObjDataArray.rotation[0], wrapper.emptyObjDataArray.rotation[1], wrapper.emptyObjDataArray.rotation[2]));
arr.layer = wrapper.emptyObjDataArray.layer*

using System;
public class EmptyObjDataWrapper {
public EmptyObjData[] emptyObjDataArray;