- Home /
Changing an element in an array. And other errors. Version 2
Ok so I have made the changes you have recommended. They are all fixed except for; the 'else' i get the errors:
invalid expression 'else'.
error expected ';'.
Also, for the 'elseif' I get the errors:
Bad array declerator. To declare a managed array the rank specifier precedes the varible's indentifier.
Array size cannot be specified in a variable decleration, 3 errors of expected ';'.
Lastly, Error invalid '=='.
I've also added some comments to my code to make it more understandable. Btw for future reference is this the best way to show new code on this website, by simply editing it or is there a better way?
void Start ()
{
int ArraySize = 20; //Actual array size will be 21x21 as it includes 0.
Random rnd = new Random();
int[,] MapArray = new int[ArraySize, ArraySize]; //Creates a 21x21 2D array (for future reference it may help to think of the map as a graph consisting of x and y coordinates).
List<int> listxCoodinates = new List<int>(); //Creates an empty list to contain all the x-coordinates.
List<int> listyCoodinates = new List<int>(); //Creates an empty list to contain all the y-coordinates.
for (int x = 0; x < ArraySize; x++) //This loop simply adds the number of elements in the array length-wise or width-wise to 2 separate lists, I know this would not work if the MapArray had different amount ofx-coordinates to y coordinates. I may fix this later.
{
listxCoordinates.Add(x); //This is a list containing all the x-coordinates of the map.
listyCoordinates.Add(x); //This is a list containing all the y-coordinates of the map.
MapArray[x, 0] = 1; //This is for creating the outer walls of the map (1 represents a wall, 0 represents the floor).
MapArray[x, MapArray.GetLength(0) - 1] = 1;
MapArray[0, x] = 1;
MapArray[MapArray.GetLength(1) - 1, x] = 1;
}
int RandomxOry = rnd.Next(0, 2); //This is for creating a variable containing either 1 or 0 chosen randomly. This will be used in deciding whether to start a wall going vertically or horizontally.
listxCoordinates.Remove(0, 1, MapArray.GetLength(0), (MapArray.GetLength(0) - 1)); //Removes a few x coodinates from the list of the possible x coordinates to be chosen from as they already are taken up as a wall or floor.
listyCoordinates.Remove(0, 1, MapArray.GetLength(1), (MapArray.GetLength(1) - 1)); //Removes a few y coodinates from the list of the possible y coordinates to be chosen from as they already are taken up as a wall or floor.
int RandomxCoordinate = rnd.Next(listxCoordinates.Count); //This gives a random index, not a random number in the list for the bot side of the map.
int RandomyCoordinate = rnd.Next(listyCoordinates.count); //This gives a random index, not a random number in the list for the left side of map.
if (RandomxOry == 0) //0 represents x side (Wall will be vertical).
{
MapArray[listxCoordinates[RandomxCoordinate], 1] = 1; //Makes MapArray at the random coordinate on the x-side equal 1 (Wall).
if (listxCoordinates[RandomxCoordinate] == 0) //if the index of the random x coodinate in the list listxCoordinates = 0 then
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate + 1); //Removes item at index in list 0 and 1.
for (int x = 1; x < MapArray.GetLength(0); x++) //This loop is for making every 0 above the random coordinate (start of the wall) equal to 1 (wall) until it reaches another 1 (wall).
{
if (MapArray[listxCoordinates[RandomxCoordinate], x + 1] == 0)
{
MapArray[listxCoordinates[RandomxCoordinate], x + 1] = 1;
}
else
{
x = MapArray.GetLength(0); //This is for ending the loop
}
}
}
elseif listxCoordinates[RandomxCoordinate] == listxCoordinates.Count - 1 //if the index of the random x coodinate in the list listxCoordinates = maximum index in list then
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate - 1); //Removes item at index in list maximum index and (maximum index - 1).
}
else
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate + 1, RandomxCoordinate - 1); //Removes item at index in list RandomxCoordinates and the items at indexes next to that RandomxCoordinates index.
}
}
if (RandomxOry == 1)
{
MapArray[1, listyCoordinates[RandomyCoordinate]] = 1;
if (listyCoordinates[RandomyCoordinate] == 0)
{
listyCoordinates.RemoveAt(RandomyCoordinate, RandomyCoordinate + 1);
}
elseif listyCoordinates[RandomyCoordinate] == listyCoordinates.Count - 1
{
listyCoordinates.RemoveAt(RandomyCoordinate);
listyCoordinates.RemoveAt(RandomyCoordinate - 1);
}
else
{
listyCoordinates.RemoveAt(RandomyCoordinate);
listyCoordinates.RemoveAt(RandomyCoordinate + 1);
listyCoordinates.RemoveAt(RandomyCoordinate - 1);
}
}
}
You should consider posting some of your errors. Very few people (myself included) have the patience to make a new project, put your script in it, and fix it. Commenting your code would also help, since code is way easier to understand if we have an idea what each code block should be doing. Add some comments and post some of your errors and I'll help you solve some of them.
Answer by Bunny83 · Nov 19, 2016 at 12:22 AM
You have several problems / potential problems:
First of all the way you treat your two dimensional array it has to be a quare map since you reuse the for loop variable for both dimensions. This is not an error but something you should keep in mind. It would be better if you used a constant instead of "20" for both array dimensions.
Line 19 and 21 you use
MapArray.GetLength()
directly as index into your array. However the largest possible index is one less than the element count. So you will get an index out of bounds error here.Your script is not complete. In line 23 you use "rnd" but it's not defined anywhere. I guess it's a
System.Random
instance?In line 26 you used the wrong max value for
Random.Range
. You add one to the element count. the max value is exclusive. To get a valid index you just have to uselistxCoordinates.Count
In lind 27 you have a typo. The property "Count" of the generic list need to be uppercase.
Now the biggest mistake. In line 28, 31, 36, 46, 55, 58 and 62 you used a single equals sign instead of a double one
==
. A single equals sign is always an assignment while the double equals is always a comparison.In line 36 and 38 you add one to the
x
value. Since "x" will go through all valid indices, adding one will cause an out of bounds error on the last element.In line 46 and 62 there's a logic error. If the "listxCoordinates" and "listyCoordinates" lists contain only valid indices (as mentioned before), those indices can never be
listxCoordinates.Count
as the highest valid index is(listxCoordinates.Count - 1)
Finally by default the Remove and RemoveAt methods of the generic List class only take one parameter. It seems you want to remove multiple elements / indices. You have to remove them with a seperate call to Remove / RemoveAt
Generally you should pay more attention to your array and index ranges. Also don't confuse =
with ==
.
btw: Those are just obvious mistakes / errors, i haven't tried to "understand" what your code actually does so there might be some more logic errors as well.
Your answer
![](https://koobas.hobune.stream/wayback/20220612100919im_/https://answers.unity.com/themes/thub/images/avi.jpg)