- Home /
Adding item to list error?
Hey, when I try to add a number 'x' into a list (x is a changing variable as its apart of a for loop) I get the error, unexpected token '(' for class, struct or interface member declaration'. As well as the same error for unexpected token ')'.
List<int> listxCoodinates = new List<int>();
List<int> listyCoodinates = new List<int>();
for (int x = 0; x < ArraySize; x++)
{
listxCoordinates.Add(x);
listyCoordinates.Add(x);
}
can you paste the whole code, e.g. ArraySize is missing. basically the error is a syntax mistake somewhere I guess. the part you pasted is ok
As @hexagonius said its a syntax error, somewhere in the script a ) is missing, review your code and find where is missing.
Answer by Axtrainz · Nov 23, 2016 at 08:00 AM
there:
void Start ()
{
public Wall prefab;
public Floor prefab;
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;
}
var RandomxOry:int = Random.Range(0, 2);
listxCoordinates.Remove(0, 1, MapArray.GetLength(0), (MapArray.GetLength(0) - 1));
listyCoordinates.Remove(0, 1, MapArray.GetLength(1), (MapArray.GetLength(1) - 1));
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
{
MapArray[listxCoordinates[RandomxCoordinate], 1] = 1;
if (listxCoordinates[RandomxCoordinate] == 0)
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate + 1); //Removes item at index in list 0 and 1.
for (int x = 1; x < MapArray.GetLength(1); x++)
{
if (MapArray[listxCoordinates[RandomxCoordinate], x + 1] == 0)
{
MapArray[listxCoordinates[RandomxCoordinate], x + 1] = 1;
}
else
{
x = MapArray.GetLength(1);
}
}
}
// HERE YOU MISSED TO ENCAPSULE THE ELSE-IF ()
else if (listxCoordinates[RandomxCoordinate] == listxCoordinates.Count - 1)
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate - 1);
}
else
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate + 1, RandomxCoordinate - 1);
}
}
if (RandomxOry == 1)
{
MapArray[1, listyCoordinates[RandomyCoordinate]] = 1;
if (listyCoordinates[RandomyCoordinate] == 0)
{
listyCoordinates.RemoveAt(RandomyCoordinate, RandomyCoordinate + 1);
}
// HERE YOU MISSED TO ENCAPSULE THE ELSE-IF ()
else if (listyCoordinates[RandomyCoordinate] == listyCoordinates.Count - 1)
{
listyCoordinates.RemoveAt(RandomyCoordinate);
listyCoordinates.RemoveAt(RandomyCoordinate - 1);
}
else
{
listyCoordinates.RemoveAt(RandomyCoordinate);
listyCoordinates.RemoveAt(RandomyCoordinate + 1);
listyCoordinates.RemoveAt(RandomyCoordinate - 1);
}
}
for (int r = 0; r < MapArray.GetLength(0); r++)
for (int c = 0; c < MapArray.GetLength(1); c++)
{
if (MapArray[r, c] == 1) // if the value of this index is 1
{
(Wall)Instantiate(yourPrefab, new Vector3(r, c, 0), Quaternion.identity);
}
}
}
Answer by steventwerd · Nov 23, 2016 at 07:33 AM
There are a bunch of errors in the full code but it is displayed bleow anyways. As far as I am aware I am not missing a '(' or ')' anyway so it would be helpful if you could help me find it please.
void Start ()
{
public Wall prefab;
public Floor prefab;
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;
}
var RandomxOry:int = Random.Range(0, 2);
listxCoordinates.Remove(0, 1, MapArray.GetLength(0), (MapArray.GetLength(0) - 1));
listyCoordinates.Remove(0, 1, MapArray.GetLength(1), (MapArray.GetLength(1) - 1));
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
{
MapArray[listxCoordinates[RandomxCoordinate], 1] = 1;
if (listxCoordinates[RandomxCoordinate] == 0)
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate + 1); //Removes item at index in list 0 and 1.
for (int x = 1; x < MapArray.GetLength(1); x++)
{
if (MapArray[listxCoordinates[RandomxCoordinate], x + 1] == 0)
{
MapArray[listxCoordinates[RandomxCoordinate], x + 1] = 1;
}
else
{
x = MapArray.GetLength(1);
}
}
}
elseif listxCoordinates[RandomxCoordinate] == listxCoordinates.Count - 1
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate - 1);
}
else
{
listxCoordinates.RemoveAt(RandomxCoordinate, RandomxCoordinate + 1, RandomxCoordinate - 1);
}
}
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);
}
}
for (int r = 0; r < MapArray.GetLength(0); r++)
for (int c = 0; c < MapArray.GetLength(1); c++)
{
if (MapArray[r, c] == 1) // if the value of this index is 1
{
(Wall)Instantiate(yourPrefab, new Vector3(r, c, 0), Quaternion.identity);
}
}
}
else-if bro, search for:
elseif listxCoordinates[RandomxCoordinate] == listxCoordinates.Count - 1
Change to:
else if (listxCoordinates[RandomxCoordinate] == listxCoordinates.Count - 1)
I changed the elseif just like you told me to but I still have the same error at the same line shown in the question.
look at my answer below I fixed all else-if.