0
\$\begingroup\$

I combined cube meshes to make a big one. I stored the info of every cube in an array within a list of chunk. Now i want to delete the cubes in the scene to only keep the combined result, but without deleting the elements in the array. After combining meshes, i want to be able do destroy cube from it. So i want to keep the info so when i click the mesh, i get the position of the cube that would be there and check if it matches wit ha cube from the array. If it does, i want to remove it from the array and regenerate the mesh without that deleted cube.

How would I do that ?

\$\endgroup\$
  • \$\begingroup\$ Why do you want to keep references to game objects in an array when those don't exist in the scene anymore? Do you need some specific information about them? Then it might be better to just store a copy of that information. \$\endgroup\$ – Philipp Jun 17 '19 at 16:02
  • \$\begingroup\$ after combining meshes, i want to be able do destroy cube from it. So i want to keep the info so when i click the mesh, i get the position of the cube that would be there and check if it matches wit ha cube from the array. If it does, i want to remove it from the array and regenerate the mesh without that deleted cube \$\endgroup\$ – Samuel Fyckes Jun 17 '19 at 17:29
  • \$\begingroup\$ Your object that describes the voxel should not be the scene object that represents the voxel in the world. Use a lighter-weight datastructure for that part, completely unrelated to your scene hierarchy. \$\endgroup\$ – DMGregory Jun 17 '19 at 17:45
  • \$\begingroup\$ Waht does that mean ? what do you mean by " your object that represent the voxel", "lighter weight datastructure" \$\endgroup\$ – Samuel Fyckes Jun 17 '19 at 17:52
1
\$\begingroup\$

If you want to keep the whole cube game object around after merging the meshes so you can restore them later, then it would be better to deactivate it with gameObject.SetActive(false) instead of destroying it. That way it stays in memory as it is and you can later reactivate it with SetActive(false).

However, for your particular use-case, that might be overkill. You would keep a lot of data about the gameObject in memory which you don't need and end up wasting a lot of RAM. All you really need to store are the coordinates of the cube within the larger merged mesh. So it would be better to define a struct which holds only the data you need about a merged cube and create an array of those struct's which you store on a component of the merged mesh.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I tought about keeping coordinates, but then how would i recombine the chunk differently if i have no mesh list to edit. Like, if i only have coordonates with no mesh, how will i recombine them ? \$\endgroup\$ – Samuel Fyckes Jun 17 '19 at 18:27
  • \$\begingroup\$ @SamuelFyckes You can derive the original mesh vertices from the position, orientation and size of the cube. \$\endgroup\$ – Philipp Jun 17 '19 at 19:46
  • \$\begingroup\$ how can I do that ? can you make a script example ? \$\endgroup\$ – Samuel Fyckes Jun 17 '19 at 20:03
  • \$\begingroup\$ @SamuelFyckes It's really not that difficult. The 8 vertices of a cube are all possible combinations of each coordinate + 0.5*size and - 0.5*size. If it's not axis-aligned, you rotate the whole mesh by the rotation. Then you translate it by the position. There are your vertices. \$\endgroup\$ – Philipp Jun 17 '19 at 20:12
  • \$\begingroup\$ Not understood. you dont speak french by any chance ? \$\endgroup\$ – Samuel Fyckes Jun 17 '19 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.