Hi! Just wanna say this is my first time posting in the forums, so I’m terribly sorry if I’m posting this in the wrong place or if I’m doing something else wrong.
I’m a relative noob at coding, but I enjoy learning more, and I’ve been trying to figure this out on my own, but I’m stumped. I have this little script that creates a number of Transform arrays based on the number of children in my game object. That part works fine (though I’m not sure it’s the best way to achieve what I ultimately need). I basically have a game object which depending on the level will have 2-5 children, which again each have between 1-12 children. I’d like to dynamically make a number of arrays equal to those 2-5 children, which should each again be the size of the children within the respective of the 2-5 children (i.e. 1-12), and then populate those arrays with those 1-12 transforms or game objects. I hope that made sense.
So I’ve tried a few things, but nothing has worked. I’ve reduced the code below to the only part that works, which is that I can create a number of arrays equal to the children in the main object. But how do I have those arrays dynamically be set to have a length equal to the children of those children, and how do i populate them with said children?
public PipeSystemWrapper [] pipeSystemController;
void Awake () {
pipeSystemController = new PipeSystemWrapper [transform.childCount];
}
}
[System.Serializable]
public class PipeSystemWrapper
{
public Transform [] pipeSystem;
}
You’d want to use a loop on the transform’s children. Get the size of each child’s transform.childCount and use that for the Transform array size. Loop over the child’s children to get the transforms.
1 Like
Thanks for the response
I’ve tried playing around with this, and I managed to sort it so that as long as I set the size of the PipeSystemWrapper array in the inspector to the relevant amount, it populates the sub arrays the way I want. Now, I feel like maybe my solution is not the most elegant or correct? But more importantly the final issue I’m having is that I’m not able to have it set the size of the pipeSystemController in any way. Whenever I do, like I’ve commented in my code, I get a null reference exception when it tries to set up the sub arrays and fill those. I just can’t ever get both to work together. Am I just missing something very obvious? (I’ve also written out all my variables in this way in an attempt to make my script more clear for my question, and made them public just for test reasons)
public Transform [] pipeSystemCounter; //I figured it was easier to just set this up to have the references more easily accesible?
public PipeSystemWrapper[] pipeSystemController;
void Awake () {
// So yeah, I set this up just to assign the parent transforms so I can reference them etc.
pipeSystemCounter = new Transform[transform.childCount];
for (int system = 0; system < pipeSystemCounter.Length; system++) {
pipeSystemCounter [system] = transform.GetChild (system);
}
// This is the bit that if I include it, it gives a NullRefException in the code further below.
// However if I exclude this, and manually assign the size in the inspector the code further below works perfectly.
// Is there no way to have it set the size of this array based on transform.childCount, and then execute the code further down?
//pipeSystemController = new PipeSystemWrapper[pipeSystemCounter.Length];
// I'm new to arrays, so if this is not the best way to do this I just don't know. this bit works seemingly?
for (int system = 0; system < pipeSystemCounter.Length; system++) {
pipeSystemController [system].pipeSystem = new Transform[pipeSystemCounter [system].childCount];
for (int i = 0; i < pipeSystemController [system].pipeSystem.Length; i++) {
pipeSystemController [system].pipeSystem [i] = pipeSystemCounter [system].GetChild (i);
}
}
}
}
[System.Serializable]
public class PipeSystemWrapper {
public Transform[] pipeSystem;
}
Using your first example, this is how you could set it up.
public class Test7 : MonoBehaviour {
public PipeSystemWrapper[] pipeSystemController;
void Awake()
{
// here the array is made for the proper size. Each element is null, though, at this point.
pipeSystemController = new PipeSystemWrapper[transform.childCount];
for(int i = 0; i < transform.childCount; ++i)
{
Transform child = transform.GetChild(i);
// here we create a new array element of type PipeSystemWrapper.
pipeSystemController[i] = new PipeSystemWrapper();
PipeSystemWrapper psw = pipeSystemController[i];
// set the size for the Transform array
// and then add the child's children.
psw.pipeSystem = new Transform[child.childCount];
for (int j = 0; j < child.childCount; ++j) psw.pipeSystem[j] = child.GetChild(j);
}
}
}
[System.Serializable]
public class PipeSystemWrapper
{
public Transform[] pipeSystem;
}
1 Like
Oh man, that works perfectly! And it seems a lot easier to work with as I expand on the script! Thank you so much! 
You’re welcome. Take care 
1 Like