Need help with organizing gameobjects into groups:

I am building a game where players can build their own ships and vehicles. How can I create a code that organizes each buildable placed in the vehicle into groups of 20?

You have to be more specfic. What is a group in your context, and what’s the purpose? Are we talking about the structure of GameObjects in the scene hierarchy, or programmatic grouping such as building arrays / lists or some other type of collection?

2 Likes

If you’re talking about the hierarchy, there’s no point, players never knows it exists.

if you’re talking about batching them in-code, you gotta get more specific about what you wanna do with that.

1 Like

I don’t understand what you’re trying to do, because why you would want to do it escapes me. But you could create a List<List> and populate it with Lists of GameObjects, which once they reach 20 you add another List. I don’t see any advantage of doing this, so you probably need to do something else though.

I would personally cheat. Throw all of the objects into a single collection. Then use a custom indexer to get them out in groups of 20.

The why would be really helpful.

1 Like

Lets suppose I have the scripts:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class AirplaneCore : MonoBehaviour {
  
    public GameObject airplaneCore;
    public List<AirplaneBuildable> buildables;
    public List<AirplaneChunk> chunks;

    void Start () {
        AirplaneChunk[] airplaneChunks = airplaneCore.GetComponentsInChildren<AirplaneChunk> ();
        foreach (AirplaneChunk chunk in airplaneChunks) {
            chunks.Add (chunk);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class AirplaneBuildable {
    public string buildableName;
    public GameObject buildableObject;
    public GameObject buildableNormalPrefab, buildableEditorPrefab, buildablePreviewPrefab;
    public Vector3 pos, rot;
    public bool destroyed = false;
    public float health = 100f, maxHealth = 100f;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class AirplaneChunk : MonoBehaviour {
    public List<AirplaneBuildable> buildablesInChunk;
}

How would take the buildables from the AirplaneCore script and place them in groups of 20, then assign each group to an AirplaneChunk’s buildablesInChunk?

You’re still not answering the why

I need them in groups of 20 so that I can merge the buildable objects into one mesh per group. that way when the airplane is hit and one of the buildables is destroyed I don’t have to reload the entire model to show the part was destroyed. If a part is destroyed, the group it was in recreates its mesh but this time without the destroyed component. That is the “Why”. It is much faster to remake a small group of buildables into a new mesh then to remake the entire airplane’s mesh.

so if you want to go straight 20 i’d go with @Kiwasi 's solution.

but in this case i’d divide them by area, identify what’s the fuselage, each wing(if theyre made up of several objects) and such.
and batch like so, if you randomize you can get the cockpit, tail wing and a bunch of random accessories in the same mesh with out any “logic” to it.

if you just randomly select them and i shoot out your wing, there’s a good change your still going to have to recalculate the mesh because every mesh has a part in that wing(be it the main part, elevons or just random lights or w/e), you see what im saying?

Each group of 20 creates its own submesh. So say if there is 60 parts to the plane, then their will be 3 submeshes. Say part number 39 is destroyed, then group 2 recreates its submesh without the destroyed component. I just need a way to output the buildables in AirplaneCore into groups of 20 that are assigned to the buildablesInChunk value in AirplaneChunk. I don’t need them to be organized by location, I just need them in groups of 20.

I created a system where the player organizes the groups manually and when one of the chunks is hit, It finds the buildable closest to the area of impact and destroys it. Then the mesh is recreated without the destroyed component. I just need to make the organizing system automatic.