• 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
Question by djfluffwug · Feb 05, 2012 at 01:49 AM · randomgenerationworldcubes

World Generation Instantiate Slowly

I have been working on some random world generation using cubes and my code has gotten to a slightly working stage. It generates a minecraftesque looking area but my problem is it instantiates 1 cube at a time through a loop.

As you can probably tell, this would be pretty slow. I was wondering if there would be a faster way to generate all the cubes instead of just instantiating 1 at a time?

I will post my ugly code below so you can get a slight idea of how it works (hopefully)

Sorry I explained this so badly but its kinda hard to explain.

      #pragma strict
  var prefab : GameObject;
  var dirt : GameObject;
  var stone : GameObject;
  var dirtstone : int;
  var objectPos : Transform;
  var chance : int;
  static var objectcount : int;
  var minObjectHeight : int;
  var maxObjectHeight : int;
  static var generationtime : int = 500;
  var lastHeight : int;
  function Start () {
  objectPos.position.x = 0;
  objectPos.position.y = 3;
  objectPos.position.z = 0;
  minObjectHeight = 1;
  }
  function Update () {
  for (objectcount = 0; objectcount < 4000; objectcount++)
  {
  dirtstone = Random.Range (1,3);
  if (dirtstone == 1){
  prefab = dirt;
  }
  if (dirtstone == 2){
  prefab = stone;
  }
  if (generationtime >0)
  {
  maxObjectHeight = Random.Range (100,105);
  generationtime -= 1;
  chance = Random.Range (1, 3);
  if (chance == 2)
      {
          objectcount += 1;
          GameObject.Find("g_Health").guiText.text

= ""+objectcount;

  for (objectPos.position.y = 0; objectPos.position.y <

maxObjectHeight; minObjectHeight++) {

  objectPos.position.y = minObjectHeight;
  prefab = Instantiate(prefab, objectPos.position,

objectPos.rotation); }

  if (objectPos.position.y > maxObjectHeight)
  {
  minObjectHeight = 1;
  objectPos.position.y = minObjectHeight;
  if (objectPos.position.x >= 16)
  {
  objectPos.position.z += 1;
  objectPos.position.x = 0;
  }
  else
  objectPos.position.x += 1;
  }
  }
  }
  }
  }
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Eric5h5 · Feb 05, 2012 at 02:05 AM

Don't use Update, just use a normal function, and loop through all the objects at once. Although using individual cubes is very slow and inefficient compared to building an optimal mesh.

Comment
flamy

People who like this

1 Show 2 · 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 djfluffwug · Feb 05, 2012 at 02:42 AM 0
Share

Thanks :D

I have now changed from update to a normal function, but I am wondering what you meant by loop through all the objects at once?

Kind Regards, - Djfluffwug

avatar image Eric5h5 · Feb 05, 2012 at 02:49 AM 0
Share

Loop through them all and don't do any delays, though I can't really tell what your code is doing since the formatting makes it very hard to read. Maybe you're not doing any delays, but in that case I don't know what you mean by not instantiating them one at a time, since that's all computers can do: a sequence of commands one at a time, very quickly.

In any case, as I mentioned, the best way is to build an optimal mesh instead of instantiating cubes.

avatar image

Answer by Tasarran · Feb 05, 2012 at 03:24 AM

I recently had to do something very similar...

What you really need to do is work toward getting your generation functionality into an Editor Script, so you can pick an option off the menu to "Generate Map".

Then, you'll have your scene all ready to go, with all that slow map creation process done pre-runtime.

Comment

People who like this

0 Show 0 · 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

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Generate minecraft alike terrain 0 Answers

What is a good way to randomly generate clouds along a path? 0 Answers

How to get neighbor from for loop in hexagonal grid ? 2 Answers

Procedural Generation Instantiating GameObjects 1 Answer

Help with Generating Random Tiles 1 Answer


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