- Home /
Storing gameObjects in an array and essentially printing them?
Is it even possible, say I have a model with 10 parts, store each individual part in an array and then have a script print them in a straight line, so you can view each part individually on its own.
How would I go about doing this? Even if I am thinking about this completely the wrong way, its the whole idea I am struggling with but any help would be appreciated.
Answer by ShadyProductions · Aug 04, 2017 at 02:00 PM
Your model (gameobject parent that has 10 children gameobjects) Give the parent a tag then you can do:
var parts = GameObject.FindWithTag("YourModelTag").GetComponentsInChildren<Transform>();
parts is your array
for(var i=0; i < parts.Length; i++) {
// skip parent
if (parts[i].tag == "YourModelTag") continue;
//create part
Instantiate(part[i], new Vector3(i, 0, 0), Quaternion.identity);
}
Appreciate the reply. It is does work to a degree. It only moves each part 1 unit apart and makes the scene extremely laggy. Any ideas :/ Not sure how to fix this
don't put it in update, but in start for example.
And change the i in new Vector3(i, 0, 0) to i + 2 for example you can choose your space between the objects.
Show me your code and I'll see if I can optimize it.
If you want them to be evenly spaced and they all have the same width, you can use (i*width, 0, 0)