Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 steventwerd · Nov 22, 2016 at 08:42 PM · listlistsunexpected token

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);
     }

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 hexagonius · Nov 22, 2016 at 10:13 PM 0
Share

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

avatar image Axtrainz · Nov 22, 2016 at 10:36 PM 1
Share

As @hexagonius said its a syntax error, somewhere in the script a ) is missing, review your code and find where is missing.

2 Replies

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

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);
                  }
              }
      }
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
avatar image
0

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);
                 }
             }
     }
Comment
Add comment · Show 3 · 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 Axtrainz · Nov 23, 2016 at 07:53 AM 0
Share

else-if bro, search for:

 elseif listxCoordinates[RandomxCoordinate] == listxCoordinates.Count - 1

Change to:

 else if (listxCoordinates[RandomxCoordinate] == listxCoordinates.Count - 1)
avatar image steventwerd · Nov 24, 2016 at 07:20 PM 0
Share

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.

avatar image Axtrainz steventwerd · Nov 25, 2016 at 12:34 AM 0
Share

look at my answer below I fixed all else-if.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Is there a way to remove array entries in the editor? 4 Answers

How do I make a list of lists? 2 Answers

from list of entries, select and add to scene beneath previous 1 Answer

How to prevent a list from being cleared at runtime? 2 Answers


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