0
\$\begingroup\$

I'm new to c# and have tried to create a nested for loop in order to check the entire array for specific numbers. I've done it in Java but need some in c#.

public GameObject WallToGen;
int[,] map = new int[9, 9];


// Start is called before the first frame update
void Start()
{
    map[1, 1] = 1;
    for (int i = 0; i < map.Length; i++)
    {
        for (int j = 0; j < map[i,j].Length; j++)
        {
            switch (map[i, j])
            {
                case 1:
                    Instantiate(WallToGen);
                    break;
                default:
                    break;
            }
        }
    }
}

I receive this error:

ERROR: Assets\Scripts\GenWall.cs(18,42): error CS1061: 'int' does not contain a definition for 'Length' and no accessible extension method 'Length' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)
\$\endgroup\$
  • \$\begingroup\$ What specific help do you need here? Where in particular are you stuck, or how specifically does the action of this code differ from what you want? \$\endgroup\$ – DMGregory Mar 22 at 23:22
  • \$\begingroup\$ I want to generate objects with the 2D array but it keeps throwing errors \$\endgroup\$ – Vortex_ Mar 22 at 23:39
  • \$\begingroup\$ Great, show us the full text of the error message and the line where it's firing. \$\endgroup\$ – DMGregory Mar 22 at 23:57
  • \$\begingroup\$ I just edited it to display the error \$\endgroup\$ – Vortex_ Mar 23 at 0:05
  • 1
    \$\begingroup\$ That's a pure syntax error. You're accessing the [I,j]th element of the array, which is an integer, and then trying to access the "Length" property of that integer — but integers don't have a property called "Length". It looks like you should take some time to read the MSDN documentation for multidimensional arrays in C#, so you can use the correct syntax for reading their bounds in each dimension. \$\endgroup\$ – DMGregory Mar 23 at 0:10
0
\$\begingroup\$

This is a pure syntax problem, which can be solved by consulting the documentation for the language you're using, or looking up the error message on our general programming sister site, StackOverflow.

You should post here only when you have code that's syntactically correct, but doesn't accomplish the gameplay effect you want. That's where game-specific expertise can be of the most use.

Here, your mistake is assuming that C# multidimensional arrays are handled as "jagged arrays" or an array-of-arrays. This is not the case - they are a single array with multiple indices.

Array.Length gives you the total number of elements in the whole array - so for your 9x9 map, that's 81.

To get the bounds in a single dimension, use the .GetLength(dimension) method, and specify the dimension as an argument (0 for outermost, 1 for the next, etc.)

int[,] map = new int[9, 9];

void Start()
{
    map[1, 1] = 1;

    for (int i = 0; i < map.GetLength(0); i++)
    {
        for (int j = 0; j < map.GetLength(1); j++)
        {
            // Don't spawn everything in one place - choose a different spawn point
            // corresponding to each i, j pair in your grid.
            Vector3 spawnPosition = new Vector3(i, 0, j);

            switch (map[i, j])
            {
                case 1:
                    // Spawn our object at this grid position, in default orientation.
                    Instantiate(WallToGen, spawnPosition, Quaternion.identity);
                    break;

                default:
                    break;
            }
        }
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.