• 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 Fabianlindfors · Aug 14, 2011 at 08:42 PM · terraininstance

Slow instancing of cubes

I try to instanciate 200 cubes at start up. They are all simple primitives with a material. But my problem is that it takes like 10s to start! And my computer is not slow! :0

 var cube : GameObject;

function Start () {

for(var z = 0;z< 2;z++) { for(var x = 0;x < 10;x++) { for(var y = 0;y < 10;y++) { cube = Instantiate(cube,Vector3(x,-z,y),Quaternion.identity);
} } } }

Thx :)

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 Eric5h5 · Aug 14, 2011 at 09:57 PM 1
Share

Format code properly using the code button. Also post more details, because that code on my computer takes .007 seconds. (Also, assigning Instantiate to a variable is pointless if you're not using that variable for anything, so you can leave it out.)

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by sneftel · Aug 14, 2011 at 10:38 PM

Instead of instantiating the same cube prefab 200 times, you're copying the previously instantiated cube each time (because you assign it back to the cube variable). It's possible that that's creating the slowdown, and it's certainly not a useful thing to do.

Comment
Add comment · 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 Bunny83 · Aug 14, 2011 at 11:31 PM 0
Share

Yep, alot people doesn't realise that Instantiate does something like CTRL+D in the editor. It clones the given object. The "source" object could be a prefab (off scene object) or an actual object in the scene.

avatar image Fabianlindfors · Aug 15, 2011 at 03:03 PM 0
Share

I didnt work :/ it takes like 3 seconds to start the game when instantiating 200 cubes and like 4s with 300 cubes. There must be something wrong!

avatar image
0

Answer by Inan-Evin · Aug 14, 2011 at 09:40 PM

I'm using something to do the same thing but i use Update function. If you can use it in your game,forget about Start

  var CubeNumber = 200;
     var cube = GameObject; // cube prefab
     
     function Update(){
     if(CubeNumber>0){
      Instantiate(cube,Vector3(x,-z,y),Quaternion.identity); // Instantiate cube all the time until CubeNumber is 0.
    CubeNumber-=1;// when 1 cube is created, substract 1 from CubeNumber
     }
     }
Comment
Add comment · 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 Eric5h5 · Aug 14, 2011 at 09:58 PM 0
Share

That would take 200 frames to instantiate all the cubes, which probably isn't acceptable in most cases. There's no reason anyway, since it takes a tiny fraction of a second to instantiate 200 cubes.

avatar image Inan-Evin · Aug 14, 2011 at 10:02 PM 0
Share

Actually i give this code just because i'm using, i create an array of seconds between 0.1 - 1.5 to create them,though.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Make a simple tree 1 Answer

Terrain addTreeInstance crash 1 Answer

Unity terrain trees dispear in front of me way to fast 1 Answer

Cant import Terrain Assets 3 Answers

How can I fix flickering in a distant terrain object with a VR flight sim for the Oculus Quest? 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