- Home /
Dynamic Array Generation via Inspector
I have a problem and have no idea how to solve this issue. I want to have a lot of public GameObject arrays and the size of the arrays should be changeable with an integer. So I need an array of the public GameObject array. Hope you can help me with that
if you want to change the size on runtime then use lists ins$$anonymous$$d of arrays.
public List<GameObject> Objects = new List<GameObject>();
// you can find with tag or type and then add to list
var gameObjectsWithTag = GameObject.FindGameObjectsWithTag("Objects");
foreach (var item in gameObjectsWithTag)
{
Objects.Add(item);
}
this list will be visible in inspector and add your GameObjects.
It's not exactly what I need, I want an array that contains other arrays, and this should update in inspector while i'm not in play mode. Thanks for your hekp but i think I know, that I need to use the Editorscript for that.
You can make arrays with in arrays using data classes.
using UnityEngine;
using System.Collections.Generic;
public class ArrayContainerTest : $$anonymous$$onoBehaviour
{
public ArrayConytainer[] NewArrayConytainer;
}
[System.Serializable]
public class ArrayConytainer
{
public List<GameObject> ObjectsList = new List<GameObject>();
}
Answer by Bunny83 · Apr 30, 2017 at 03:22 PM
You just want to use a simple serializable custom class. Like i mentioned in this answer over here. Just replace the string array with whatever you want.
edit
Here's an example:
[System.Serializable]
public class GameObjectArray
{
public GameObject[] objects;
}
public class YourComponent : MonoBehaviour
{
public GameObjectArray[] arrays;
void Start()
{
for(int i = 0; i < arrays.Length; i++)
{
for(int n = 0; n < arrays[i].objects.Length; n++)
{
Debug.Log("Pos: " + arrays[i].objects[n].transform.position;
}
}
}
}
I tried this, but it doesn't work with GameObject Arrays
I could create such an array as GameObject, but couldn't access the containing GameObjects
Of course it works. I added an example that does work for sure. The example iterates through all arrays and through all objects in each array and prints out the position of each object.
Oh well thanks I dunno what I did wrong today, now it works xD