Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to Duplicate 500,000 Cubes?

Discussion in 'Scripting' started by Topagon, Apr 26, 2023.

  1. Topagon

    Topagon

    Joined:
    Apr 18, 2023
    Posts:
    2
    I'm trying to make 500,000 duplicates of my 3d cube through script but the game engine keeps rejecting every script I write. I've tried instantiate options but they did not work. Does anyone know how?
     
  2. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    For loop iterating a game object instantiation….should do it.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    What does this mean? Compiler errors? Runtime errors?

    Helps to show us your code and what errors you're getting.

    Mind you instantiating 500K cubes is going to kill performance. Is this a Minecraft clone situation?
     
    Kurt-Dekker likes this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,185
    See if this works for you. Just be aware it's incredibly demanding to take this approach. I'm on an AMD 5950X with an RTX 3070 and I'm getting less than one frame per second. Games like Minecraft use clever techniques to hit the performance that they do. You can't just brute force it like this.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Cubes : MonoBehaviour
    4. {
    5.     public GameObject cubePrefab;
    6.  
    7.     void Start()
    8.     {
    9.         for (var index = 0; index < 500000; index++)
    10.             Instantiate(cubePrefab, new Vector3(Random.Range(-100f, 100f), Random.Range(-100f, 100f), Random.Range(-100f, 100f)), Quaternion.identity);
    11.     }
    12. }
    upload_2023-4-25_22-16-46.png
     
    Last edited: Apr 26, 2023
    All_American likes this.
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Just to add to what others have implied here: Your PC does not matter. The game wont run with 500k individual gameobjects. For large numbers of entities, DOTS would be your best bet. But even then, while it can easily handle tens of thousands of entities, 500k is a large number. Give us some context to work with: why do you need this? This seems like an XY Problem.
    If this is indeed an attempt at creating a voxel based world, it is the completely wrong approach. Voxel based worlds are not made of individual objects. That wouldnt run, at all. Instead the underlying data is sampled and visualized according to the way in which its supposed to look like, ie a blocky way. No individual block objects would be part of that.

    But for the information given you already got the appropriate information.
    If you need any further help you will want to explain your actual usecase.
     
  6. Topagon

    Topagon

    Joined:
    Apr 18, 2023
    Posts:
    2
    Thanks to everyone that replied so quickly. The game I am designing involves digging so my idea was to have a Mao made of thousands of tiny game cubes. The cubes would disappear once the player activated digging animation and walked into them. This would make the digging look slow and closer to reality. I didn't want large blocks of dirt to disappear once every 5 hits like Minecraft does. After more reading I realized the problem from a code as well as a rendering perspective. l very new to this and I am sure it's the wrong approach but I'm learning as fast as I can. My idea now is to have large cubes and once a player has struck with enough hits an animation would show them slowly digging through the dirt.
     
  7. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    You could just render the first layer….you don’t need to have that many cubes. Just the layer you see and instantiate the next layer when you absolutely need to.
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,185
    Minecraft behaving that way isn't a limitation of the approach that it uses. Notch could have just as easily had the cubes be much smaller. Minecraft works by combining all of the visible cubes in a vicinity of a chunk into a single mesh and renders that mesh. When a change occurs to the cubes in the chunk the mesh is regenerated.
     
  9. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    He could do the same thing with combinemeshes.
     
  10. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    101
    He could combine, but can he split that easily afterwards? :D
     
  11. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    there’s gotta be a way.
     
  12. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Voxels are indeed the correct approach for this, if you need a general purpose solution. However, be aware that voxels are one of the most complex topics you will encounter in game development. They involve efficient data structures, real time mesh editing, as well as things like multithreading and certain optimizations you will have to include from the get go. This is not a beginner topic. Each of those topics alone would qualify as advanced.

    If you have a premade map with fixed spots where the player can dig, you dont need a general purpose solution however. You could just fake it, for example by having the tunnel consist of several individual custom made meshes, of which you disable the one the player was digging at after he is done. This would show some actual progress in him digging the tunnel and would have a very similar effect compared to voxels, but does require orders of magnitude less complexity. Of course this is not a general purpose solution and rather for individual spots you want to be able to change in a premade map. But for most games that, or something along those lines, is more than enough.