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 /
This question was closed Aug 01, 2012 at 07:20 AM by Chimera3D for the following reason:

I made a silly mistake.

avatar image
0
Question by Chimera3D · Aug 01, 2012 at 07:02 AM · performanceissue

Unity Code Performance Issue

The for loop in this code is causing Unity to crash, why would that be? Here's the code:

 var yLimit : int;
 var zLimit : int;
 
 var gridStart : Vector3;
 
 var spacing : float = 10;
 var gridPos : Transform;
 
 function Start () {
 
 gridStart = transform.position; 
 
 for (var z = 0; z < zLimit; z++) {
 
 for (var y = 0; y < yLimit; y--) {
 
 var newGridPos = Instantiate(gridPos, Vector3 (0, z * spacing, y * spacing), Quaternion.identity);
 
 newGridPos.transform.parent = transform;
 
 }
 
 }
 
 }
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Aug 01, 2012 at 07:05 AM

You're creating yLimit * zLimit objects every frame. Naturally this will cause major issues. I would suggest not doing that....

Also, your inner loop is infinite, assuming yLimit is a positive number, which is an even more major problem.

Comment
Add comment · Show 6 · 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 Chimera3D · Aug 01, 2012 at 07:07 AM 0
Share

even with function Start it would cause Unity to fail

avatar image Eric5h5 · Aug 01, 2012 at 07:08 AM 0
Share

I was editing my answer when you posted that. ;)

avatar image Chimera3D · Aug 01, 2012 at 07:13 AM 0
Share

Strangely enough the instantiate part of the code isn't causing it to be a performance issue, the for loop is. Can someone tell me what is going on?

avatar image Eric5h5 · Aug 01, 2012 at 07:15 AM 0
Share

Read the second paragraph of my answer.

avatar image oxium · Aug 01, 2012 at 07:17 AM 0
Share

you should change your loop code to be : for (var z = 0; z <= zLimit; z++)

just in case you forgot to properly set zlimit in the UI and you end up with the default value (0) that would loop for ever.. ;)

same for ylimit

Show more comments

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

Script Efficiency 1 Answer

Mover Player With seesaw 1 Answer

Nicknames in multiplayer 1 Answer

Horizontal Slider issue 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