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 GarrettM · Dec 19, 2014 at 02:30 AM · c#for-loopfor loopwhats-wrong

Infinite For Loops? (C#) [ANSWERED]

So what I'm trying to do here is make a floor out of tiles (there's a reason for that that's unrelated to the issue) so I have a prefab I dropped in and then I'm trying instantiate 100 of them. What happens when I click play is that it just never loads and I have to force quit the program. I know it CAN instantiate 100 because I've done that and had no issues, it just put them in the wrong spots so I had to change the code around. It's not a hardware issue that it's just taking a long time.

I'm sure I just messed up the logic of the for loops or some amateur mistake so I figured I'd drop this in here and see if some more experienced programmers could point out what I did wrong.

Thanks in advance for your help!

 using UnityEngine;
 using System.Collections;
 
 public class DiscoFloor : MonoBehaviour {
 
     public Transform floorTile;
 
     private float xMin = -450.0f;
     private float xMax = 450.0f;
     private float zMin = -450.0f;
     private float zMax = 450.0f;
     private int i;
     private int iTwo;
     private Vector3[] placementArr;
 
 
 
     void Start() {
 
         placementArr = new Vector3[100];
         //placementArr [0] = new Vector3 (-450.0f, 0, 450.0f);
         //placementArr [100] = new Vector3 (450.0f, 0, -450.0f);
 
         for (int i = 0; i < placementArr.Length; i++){
 
             for (float xMin = -450.0f; xMin < xMax; xMin = xMin + 100.0f){
 
                 for (float zMin = -450.0f; zMin < zMax; zMin = zMin + 100.0f){
 
                     placementArr[i] = new Vector3 (xMin, 0, zMin);
 
                     for (int iTwo = 0; iTwo < placementArr.Length; iTwo++) {
                         Instantiate(floorTile, placementArr[iTwo], Quaternion.identity);
                     }
                 }
             }
 
 
         }
 
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
Comment
Add comment · Show 2
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 VesuvianPrime · Dec 19, 2014 at 02:30 AM 0
Share

The loops look ok, but personally I would avoid float loops because of rounding.

Regardless, put a debug inside the loop ins$$anonymous$$d of an instantiate call. If the loops run then you know the problem is the instantiation, otherwise you know its the loops.

avatar image zaid87 · Dec 19, 2014 at 03:31 AM 0
Share

Although I'm not sure if this is the cause. I'm pretty sure you're instantiating way more than just 100 object. The innermost loop would run 100 times, but then it's inside 3 other loops, with the outermost will also run 100 times. Unless I'm reading the thing wrong, pretty sure that's what happened.

2 Replies

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

Answer by Kartik1607 · Dec 19, 2014 at 06:19 AM

Let me try to break up your code and see what is happening.

The First loop, runs 100 times.

 for (int i = 0; i < placementArr.Length; i++){...

The next two loops find the position of the tile.

 for (float xMin = -450.0f; xMin < xMax; xMin = xMin + 100.0f){
  
  
             for (float zMin = -450.0f; zMin < zMax; zMin = zMin + 100.0f){
  
                      placementArr[i] = new Vector3 (xMin, 0, zMin);

The last loop runs 100 times and each time Instantiates floorTile at same position. So there are 100 floorTiles at position xMin and zMin.

 for (int iTwo = 0; iTwo < placementArr.Length; iTwo++) {
                          Instantiate(floorTile, placementArr[iTwo], Quaternion.identity);

Since first loop runs 100 times, there are 100*100 objects at one location and 100*10*10*100 total objects!

Lets try to create a floor.

Basic Idea : Create a floor tile at X and Z position. Change X and Z. Repeat.

We need two variable, 1) One would keep track of row 2) Second would keep track of column.

So, create a floor tile for entire row. Go to next row. Create tile for that row. Repeat.

So, lets take some variables.

     private float xMax = 450.0f;
     private float zMax = 450.0f;
 
 xMin would work as row.
 zMin would work as column.
 

Now, we need to loop xMin (row) from starting to xMax. This will take care of the rows.

         for (float xMin = -450.0f ; xMin < xMax; xMin = xMin + 100.0f) 
         {
             ...
         }

And to take care of columns keep incrementing zMin inside xMin loop.

             for( float zMin = -450.0f; zMin < zMax ; zMin = zMin + 100.0f)
             {
                            ...
             }
         

Instantiate the object inside the zMin loop at position (xMin,0,zMin).

 Instantiate(floorTile, new Vector3(xMin,0,zMin), Quaternion.identity);

That's all you need.

Full Code :

 public Transform floorTile;
 
     private float xMax = 450.0f;
     private float zMax = 450.0f;
 
     void Start() {
         
         for ( float xMin = -450.0f; xMin < xMax; xMin = xMin + 100.0f) 
         {
             for( float zMin = -450.0f ; zMin < zMax ; zMin = zMin + 100.0f)
             {
                 Instantiate(floorTile, new Vector3(xMin,0,zMin), Quaternion.identity);
             }
         }
     }


Comment
Add comment · Show 1 · 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 GarrettM · Dec 19, 2014 at 05:16 PM 0
Share

Thanks, this fixed it. Also thanks for walking me through it step by step so I understand what's going on. I have a tendency to over-complicate things.

avatar image
2

Answer by Bunny83 · Dec 19, 2014 at 04:56 AM

Your loops don't make much sense to me. You have 4 nested loops and two are iterating over the same length (placementArr.Length). Since you have 100 position values in your placementArr. That means you try to instantiate:

 100 * 10 * 10 * 100 == 1,000,000 objects

You most likely want something like:

 int i = 0;
 for (float xMin = -450.0f; xMin < xMax; xMin = xMin + 100.0f)
 {
     for (float zMin = -450.0f; zMin < zMax; zMin = zMin + 100.0f)
     {
         placementArr[i] = new Vector3 (xMin, 0, zMin);
         Instantiate(floorTile, placementArr[i], Quaternion.identity);
         i++;
     }
 }

I don't quite understand what that "placementArr" array is actually good for... You could just do:

 for (float xMin = -450.0f; xMin < xMax; xMin = xMin + 100.0f)
 {
     for (float zMin = -450.0f; zMin < zMax; zMin = zMin + 100.0f)
     {
         Vector3 pos = new Vector3 (xMin, 0, zMin);
         Instantiate(floorTile, pos, Quaternion.identity);
     }
 }
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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Autocomplete a for-loop in MonoDevelop? 1 Answer

forloop running more than said amout of times 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 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