- Home /
How to merge an arrays into arrays inside an array?
I can't access an array inside an array in unity inspector. so instead I have an idea to make those array into an array or called array inside an array
I have these 2 public arrays:
public GameObject[] darkTiles;
public GameObject[] whiteTiles;
and then add another array that store these arrays:
GameObject[] tilesOb = new GameObject[2];
tilesOb[0] = darkTiles;
tilesOb[1] = whiteTiles;
easy, right? well, not that fast, unity can't do the code, can you help me to answer a solution?
Answer by AbandonedCrypt · Feb 15, 2021 at 03:12 PM
You are trying to put an array into a GameObejct reference.
This will probably compile:
GameObject[][] tilesOb = new GameObject[][2];
tilesOb[0] = darkTiles;
tilesOb[1] = whiteTiles;
However I don't think this is good use of arrays. Why not use a multi-dimensional GameObject[,]
array instead of an array of arrays? Arrays of arrays are a good way to get lost very fast and they are barely maintainable and error-prone as F.
I think I have mistaken I thought gameObject[,] was array inside an array
but, still, both methods using array inside an array or 2-dimensional array is made an error, which is a headache
maybe can you make an example with GameObject[ , ]; who knows it will fixed the eror
If i'm honest, I would think about your architecture again. If you need an array of arrays chances are good you can improve on your structure. I dont know what you want to use this for, but yea.
$$anonymous$$aybe try saying what you want to achieve, then I can give you some pointers
Answer by xxmariofer · Feb 15, 2021 at 03:22 PM
This is the only way i can think right now for serializing 2 arrays in the inspector but probably there is another way
public YourSerializableArray[] darkTiles;
[System.Serializable]public struct YourSerializableArray
{
public GameObject[] whiteTiles;
}
btw, just saying I want it can displays in inspector because I want it to can drag and drop so I can control the element placement
this is the way (as a mandoliarn would say). this shows in inspector. Another way would be to create a serializable class
This is copied from the unity answer thread i linked in another question of the OP, also this is not answering the question, since inspector display wasn't asked for
What? You really think I somehow found one of your old answers and I copied it? Do you think serializing an structure is something someone with basic c# knowledge would not think of? and literally the question is
I can't access an array inside an array in unity inspector.
is literally what he says he is not able to do??
The question is "How to merge an arrays into arrays inside an array?". His code samples show him trying to create a nested array and he says he is getting an error message and it wont compile.
his previous question was "How to make a public game object array inside an array (or 2D array) that can drag and drop to fill?" (also today) , which is the question you answered here, however weirdly enough I have linked the original thread in there which has your answer. You answering the 2nd question in this question thread and saying you haven't seen it just looks like a weird coincidence to me.
Answer by abyboyganteng · Feb 15, 2021 at 04:17 PM
My own solution(based on @AbandonedCrypt answer):
public GameObject[] darkTiles;
public GameObject[] whiteTiles;
GameObject[,] tilesOb = new GameObject[11, 2];
void Start()
{
for(int i = 0; i <= 11; i++)
{
tilesOb[i, 0] = darkTiles[i];
}
for (int i = 1; i <= 11; i++)
{
tilesOb[i, 1] = whiteTiles[i];
}
}