Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DranrebKing · Feb 01, 2019 at 12:50 AM · renderingbugcrashgameobjectsmesh renderer

Generating too many blocks crashes the game (Both exe and in editor)

I have created this minecraft-ish voxel game where the player can create a voxel map. I have a block prefab (nothing much of the ordinary) - it has a box collider, a texture, and a mesh renderer.

On my code, it just as simple as this:

 public void GenerateTerrain()
     {
         for(int x = 0; x < mapSizeX; x++)
         {
             for(int z = 0; z < mapSizeZ; z++)
             {
                 GameObject groundBlock = Instantiate(groundBlocks[0], new Vector3(x, transform.position.y, z), Quaternion.identity);
                 groundBlock.transform.parent = transform;
             }
         }
     }



I think this works fine if you put in 32x32. However if the player tries to generate a 128x128 (16k blocks) or greater and tries moving around the map, the game crashes and even shutdowns the computer (i know it's scary).

I tried using the built in occlusion culling, it kinda increased the performance "a bit", however it still crashes.

How can I fix this?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ymrasu · Feb 01, 2019 at 01:00 AM 0
Share

Its better to build a single mesh ins$$anonymous$$d of each block being a gameobject. This is an good place to start learning voxels: $$anonymous$$sTV

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cornelis-de-Jager · Feb 01, 2019 at 12:58 AM

Unfortunately Standard Unity is not optimized well for creating many blocks. Its one of the Unity's downfalls, however, all hope is not lost.


Unity has already created the Entity Component System, which is a different way of programming in C# that optimizes memory allocation and retrieval, thus giving HUGE boosts to performance. You no longer use object orientated programming, however, so the learning curve is steep, but performance is worth the while.


Here is a video of a stress test where 500K cubes were spawned. CLICK ME.


If you want to use the ECS system you will need to download the correct versions of unity and enable it. CLICK ME TO SEE HOW

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DranrebKing · Feb 01, 2019 at 01:10 AM 0
Share

I have watched the videos. It's kinda neat. However I don't know how to implement ECS into my code though. How did you manage to get it all working in generating those 500k cubes?

avatar image
0

Answer by AnOrdinarySandwich · Feb 01, 2019 at 01:11 AM

If you're not interested in switch to ECS, perhaps something simple like a proximity zone around the player that causes any groundBlock out of range to disable its collider. Could easily be set up with a trigger collider on the player and then use OnTriggerEnter to enable the block colliders, and OnTriggerExit for disabling (as well as being the blocks default state) the block colliders. Just a thought! :)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DranrebKing · Feb 01, 2019 at 01:25 AM 0
Share

Nice idea! :)

I tried this one:

 private void OnTriggerEnter(Collider other)
     {
         if(other.CompareTag("PlayerParent"))
         {
             mesh.enabled = true;
             collider.enabled = true;
 
             Debug.Log("Collided");
         }
     }
 
 
 
     private void OnTriggerExit(Collider other)
     {
         if (other.CompareTag("PlayerParent"))
         {
             mesh.enabled = false;
             collider.enabled = false;
         }
     }


However it seems it doesn't actually detected the colliders? I created a sphere collider in my playerobject, tagged it as "PlayerParent", and set the colliders as a "isTrigger". However the 'collided' in the console doesn't seem to appear.

avatar image DranrebKing · Feb 01, 2019 at 01:31 AM 0
Share

Okay I got the colliders working, I forgot to add a rigidbody on my player parent... however, the ontrigger doesn't seem to work.

avatar image DranrebKing · Feb 01, 2019 at 01:35 AM 0
Share

Here's my updated code:

 private void Start()
     {
         mesh = GetComponent<$$anonymous$$eshRenderer>();
         collider = GetComponent<BoxCollider>();
 
         mesh.enabled = false;
     }
 
 
     private void OnTriggerStay(Collider other)
     {
         if (other.CompareTag("PlayerParent"))
         {
             mesh.enabled = true;
         }
     }
 
 
 
     private void OnTriggerExit(Collider other)
     {
         if (other.CompareTag("PlayerParent"))
         {
             mesh.enabled = false;
         }
     }


At the start of the scene, the blocks that were kinda far from the player, their mesh renderers got disabled. However when I approach them, it won't enable them (it should). Also when I leave the blocks of which the mesh renderer is enabled, it did not disappear also (it should since onTriggerExit will be triggered right?)

avatar image AnOrdinarySandwich DranrebKing · Feb 01, 2019 at 02:01 AM 0
Share

Hi, it seems like the OnTriggerEnter (I wouldn't recommend using OnTriggerStay, as it will be called all the time on all the blocks within range of the collider) and OnTriggerExit are on the blocks, not on the player. I assume this from the tag check, looking for the other object to be a PlayerParent.

If so, try putting them on the parent ins$$anonymous$$d.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

131 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity doesn't scale Blit-to-screen render on X-axis? 0 Answers

Unity on my laptop keeps crashing when I press play, but works on a different computer with the same version of Unity 2 Answers

Text Appearance Glitch in Unity UI 5.3 3 Answers

Unity instantly crashes when switching from game to scene view. 2 Answers

Unity 2.6.1f crashes 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges