Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Hugs Are Drugs · Feb 08, 2014 at 06:59 PM · meshproceduralprocedural mesh

Why does this script put Unity into a not responding state?

I can't figure out why this script is constantly crashing Unity and I'm tired of trying to figure it out, I'd like some help.

using UnityEngine; using System.Collections;

 public class TerrainGenerator : MonoBehaviour
 {
     public GameObject voxel;
     public float voxelSize;
     public float chunkHeight;
     public int chunkSize;
     
     float x;
     float y;
     void Awake ()
     {
         Mesh mesh = new Mesh ();
         Vector3 [] vertices = new Vector3 [4]
         {
             new Vector3 (0, 0, 0), new Vector3 (voxelSize, 0, 0), new Vector3 (voxelSize, 0, voxelSize), new Vector3 (0, 0, voxelSize)
         };
         int [] triangles = new int [6]
         {
             1, 3, 0, 1, 3, 2
         };
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         for (float i = 0; i < chunkSize; i += voxelSize)
         {
             x = 0;
             y += 0.1f * voxelSize;
             for (float f = 0; f <= chunkSize; f += voxelSize)
             {
                 x += 0.1f * voxelSize;
                 Vector3 position = new Vector3 (f, Mathf.Round (Mathf.PerlinNoise (x, y) * chunkHeight) * voxelSize, i);
                 GameObject voxelTransform = (GameObject) Instantiate (voxel, position, Quaternion.identity);
                 MeshFilter mf = voxelTransform.GetComponent <MeshFilter> ();
                 mf.mesh = mesh;
             }
         }
     }
 }
Comment
Add comment · Show 4
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 getyour411 · Feb 08, 2014 at 07:04 PM 0
Share

get rid of the semi-colons after the } on 16 and 20. Are your #s in the Inspector large?

avatar image Jamora · Feb 08, 2014 at 09:10 PM 0
Share

Your chunkSize is probably too high. Remember: your inner for loop is run chunkSize*chunkSize (assu$$anonymous$$g voxelSize is one) times within one frame, and Instantiate isn't the fastest function out there... I don't know how efficient $$anonymous$$athf.PerlinNoise is.

avatar image Hugs Are Drugs · Feb 08, 2014 at 09:28 PM 0
Share

Apparently chunksize was too large, but I think there's something wrong with my code now. When I set chunksize to 1 it for some reason instantiates 6 objects? The problem is that it's generating 6 times as many as it should. and also, my game runs at a very low framerate even with 6 of the GameObjects, why?

avatar image Hugs Are Drugs · Feb 08, 2014 at 09:33 PM 0
Share

Forget what I just said about it spawning 6x as many, I had voxel sets to not 1. But can somebody explain why the game runs so slow with just a few objects? Is it because of how inefficient instantiate is? I am confused. I'm really just messing around and in the end wanted to develop this into just generating one large mesh and store points in a large array.

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

Simple plane generation 1 Answer

Determining where a mesh triangle faces 1 Answer

How do I calculate the UV's for a procedurally generated shape? 2 Answers

What is wrong with this mesh editing code? 0 Answers

Editor lags when coming out of play mode...? 0 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