x


What is wrong in my code - Unity freezes

Hi I made a code that I will eventually use for stacking objects, but are at the moment just testing if it executes correctly which it doesn't and I can't seem to figure out what is wrong.

The Code:

var countx:int=0;
var county:int=0;
var countz:int=0;

function Update () {

    while (county < 101){
        ++countx;
        Debug.Log("1: "+countx+","+county+","+countz);
        if (countx==200) {
            ++countz;
            Debug.Log("2: "+countx+","+county+","+countz);
            if (countz==200) {
                ++county;
                countz=1;
                Debug.Log("3: "+countx+","+county+","+countz);
            }
            if (county< 100)countx=1;           
        }
        Debug.Log("4: "+countx+","+county+","+countz);
    }   
}
more ▼

asked Mar 26 '10 at 12:28 PM

Asle gravatar image

Asle
37 8 9 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Are you trying to achieve something like this?:

for (var countY=0; countY < 100; ++countY) {
    for (var countZ=0; countZ < 200; ++countZ) {
        for (var countX=0; countX < 200; ++countX) {
            // Debug.Log(countX + ", " + countY + ", " + countZ);
        }
    }
}

(i.e. iterate through every point in a 200x200x100 structure?)

Using nested 'for' loops like this is the typical method for iterating through a 2d or 3d set, however the figures you have give a rather large number of total iterations. For this reason, I have the Debug.Log line commented out in my example, because if you run it, that code will generate 4 million debug.log lines (which may cause your computer to seem as though it has locked up while printing them all to the console).

Note, if you're planning to stack gameobjects using something like this, you'll end up with 200 x 200 x 100 objects - that's 4 million gameobjects - something that is not really feasible to work with in Unity.

more ▼

answered Mar 26 '10 at 12:43 PM

duck gravatar image

duck ♦♦
41.4k 95 152 415

I am making a kind of a stesstest for unity to test how many objects I can have in a scene at the same time, I am not generating all 4 million objects at the same time, but I wanted to have an array of positions where I could put the objects as I generate them.

I generate objects by pressing the mousebutton.

Mar 28 '10 at 06:01 AM Asle
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x183
x17

asked: Mar 26 '10 at 12:28 PM

Seen: 660 times

Last Updated: Mar 26 '10 at 02:56 PM