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 Ben 18 · Nov 10, 2010 at 06:44 AM · javascriptinstantiate

Instantiate loading very slowly

I am trying to set up a basic map with 256x256. However the load time is astronomical. I've tried reducing the size of the array but it is still way slower than it needs to be. What have I done wrong?

var mapSizeX = 256; var mapSizeZ = 256; var mapArray = MultiDim.StringArray(mapSizeX, mapSizeZ); var createMap : Transform;

 function Start () {

 for (var x = 0; x < mapSizeX; x++) {   
     for (var z = 0; z < mapSizeZ; z++) {
         mapArray[x,z] = (x) + " " + (z);
         var pos = Vector3 (x, 0, z);
         var objecta = Instantiate(createMap, pos, Quaternion.identity);
                     objecta.name = mapArray[x,z];
     }
 }

}

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by unitynoob · Nov 10, 2010 at 06:55 AM

Firstly Instantiate is a slow function that you may want to reduce usage in a big nested loop, and how slow it is depends on how complicated the object it is instantiating. Instantiating a cube is much faster than a monster with rigidbody, collider and a couple of scripts.

Secondly you may try to reduce to 'new' a variable in a loop. Reuse them, namely 'pos' and 'objecta' in your loop.

Lastly if you do not need your map be ready in a frame, you may spread the instantiations across frames in loops by 'yield', such that you can reduce the degree of lagging.

Comment
Add comment · 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
1

Answer by Eric5h5 · Nov 10, 2010 at 08:01 AM

256*256 = 65,536 objects, which is way way more than you would ever want. You should reduce that by a couple orders of magnitude. For such a large map, you'd be better off constructing it as a series of meshes using the Mesh class, where each mesh would cover, say, 64x64 squares. This is a quite a bit more complicated than just instantiating prefabs, but Unity can't feasibly handle tens of thousands of separately instantiated objects like that. Another possibility, depending on what you're doing, is to instantiate only the map immediately around the player, and remove the squares that are out of view.

Comment
Add comment · 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
0

Answer by soulburner · Nov 20, 2012 at 02:51 AM

Consider re-using objects that moved off screen instead of killing and instantiating new ones

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Instantiate object in for loop with array 2 Answers

Instantiate without (clone) in JS. 1 Answer

Unity Frame Drop From Instantiating Prefab that has script 0 Answers

Instantiating infinity copies of the same prefab? 1 Answer

Instatiate inside OnTriggerEnter gives unwanted delay 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