Search Unity

error CS1547: Keyword `void' cannot be used in this context

Discussion in 'Scripting' started by wiredin, Nov 26, 2015.

  1. wiredin

    wiredin

    Joined:
    Nov 26, 2015
    Posts:
    10
    Hi guys I need some help, I was doing a new script for my project and just when i was finishing i encounter this error on my editor.
    I tried removing and adding the close brackets but still unable to solve the issue can anyone help me will really appreciate :)

    error CS1547: Keyword `void' cannot be used in this context

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class maze : MonoBehaviour {
    5.     [System.Serializable]
    6.     public class Cell {
    7.         public bool visited;
    8.         public GameObject north;
    9.         public GameObject east;
    10.         public GameObject west;
    11.         public GameObject south;
    12.  
    13.     }
    14.  
    15.     public GameObject wall;
    16.     public float wallLength = 1.0f;
    17.     public int xSize = 5;
    18.     public int ySize = 5;
    19.     private Vector3 initialPos;
    20.     private GameObject wallHolder;
    21.     private Cell[] cells;
    22.     public int currentCell = 0;
    23.     private int totalCells;
    24.  
    25.     // Use this for initialization
    26.     void Start () {
    27.         CreateWalls ();
    28.     }
    29.  
    30.     void CreateWalls (){
    31.         wallHolder = new GameObject ();
    32.         wallHolder.name = "Maze";
    33.  
    34.         initialPos = new Vector3 ((-xSize / 2) + wallLength / 2, 0.0f, (-ySize / 2) + wallLength / 2);
    35.         Vector3 myPos = initialPos;
    36.         GameObject tempWall;
    37.  
    38.         //For X Axis
    39.         for (int i = 0; i < ySize; i++) {
    40.             for (int j = 0; j <= xSize; j++) {
    41.                 myPos = new Vector3 (initialPos.x + (j*wallLength)-wallLength/2,0.0f,initialPos.z+(i*wallLength)-wallLength/2);
    42.                 tempWall = Instantiate(wall,myPos,Quaternion.identity) as GameObject;
    43.                 tempWall.transform.parent = wallHolder.transform;
    44.             }
    45.         }
    46.  
    47.         //For Y Axis
    48.         for (int i = 0; i <= ySize; i++) {
    49.             for (int j = 0; j < xSize; j++) {
    50.                 myPos = new Vector3 (initialPos.x + (j*wallLength),0.0f,initialPos.z+(i*wallLength)-wallLength);
    51.                 tempWall = Instantiate(wall,myPos,Quaternion.Euler(0.0f,90.0f,0.0f)) as GameObject;
    52.                 tempWall.transform.parent = wallHolder.transform;
    53.             }
    54.         }
    55.         createCells ();
    56.     }
    57.  
    58.  
    59.     void createCells () {
    60.         GameObject[] allWalls;
    61.         int children = wallHolder.transform.childCount;
    62.         allWalls = new GameObject[children];
    63.         cells = new Cell[xSize * ySize];
    64.         int eastWestProcess = 0;
    65.         int childProcess = 0;
    66.         int termCount = 0;
    67.  
    68.         //Get All the children
    69.         for (int i = 0; i < children; i++) {
    70.             allWalls = wallHolder.transform.GetChild(i).gameObject;
    71.         }
    72.         //Assigns walls to the cells
    73.         for (int cellprocess = 0; cellprocess < cells.Length; cellprocess++) {
    74.             cells[cellprocess] = new Cell ();
    75.             cells[cellprocess].east = allWalls[eastWestProcess];
    76.             cells[cellprocess].south = allWalls[childProcess+(xSize+1)*ySize];
    77.             if (termCount == xSize) {
    78.                 eastWestProcess+=2;
    79.                 termCount = 0;
    80.             }
    81.             else
    82.                 eastWestProcess++;
    83.  
    84.             termCount++;
    85.             childProcess++;
    86.             cells[cellprocess].west = allWalls[eastWestProcess];
    87.             cells[cellprocess].north = allWalls[(childProcess+(xSize+1)*ySize)+xSize-1];
    88.         }
    89.         Createmaze ();
    90.     }
    91.  
    92.     void Createmaze () {
    93.         GiveMeNeighbour ();
    94.     }
    95.  
    96.     void GiveMeNeighbour() {
    97.         totalCells = xSize * ySize;
    98.         int length = 0;
    99.         int[] neighbours = new int[4];
    100.         int check = 0;
    101.         check = ((currentCell + 1) / xSize);
    102.         check -= 1;
    103.         check *= xSize;
    104.         check += xSize;
    105.         //west
    106.         if (currentCell +1 < totalCells && (currentCell+1) != check) {
    107.             if (cells[currentCell + 1].visited == false) {
    108.                 neighbours[length] = currentCell+1;
    109.                 length++;
    110.             }
    111.         }
    112.  
    113.         //east
    114.         if (currentCell - 1 >= 0 && currentCell != check) {
    115.             if (cells[currentCell - 1].visited == false) {
    116.                 neighbours[length] = currentCell-1;
    117.                 length++;
    118.             }
    119.         }
    120.  
    121.         //north
    122.         if (currentCell + xSize < totalCells) {
    123.             if (cells[currentCell +xSize].visited == false) {
    124.                 neighbours[length] = currentCell+xSize;
    125.                 length++;
    126.             }
    127.         }
    128.  
    129.  
    130.         //south
    131.         if (currentCell - xSize >= 0) {
    132.             if (cells[currentCell - xSize].visited == false) {
    133.                 neighbours[length] = currentCell-xSize;
    134.                 length++;
    135.             }
    136.         }
    137.         for (int i = 0; i < length; i++)
    138.             Debug.Log (neighbours );
    139.  
    140.     // Update is called once per frame
    141.     void Update () {
    142.  
    143.     }
    144. }
     
    Last edited: Nov 26, 2015
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. Sheikz

    Sheikz

    Joined:
    Nov 1, 2015
    Posts:
    24
    Which "void" is causing the issue?
     
  4. wiredin

    wiredin

    Joined:
    Nov 26, 2015
    Posts:
    10
    void update is causing the issue but i still can't figure out what is wrong with my code
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you properly format your code here, it would have been easy for us to spot, that you are missing a } before the "void Update". That's why you should always use code tags! Most don't even check code in the forum that is not properly formatted.
     
    Kiwasi and wiredin like this.
  6. wiredin

    wiredin

    Joined:
    Nov 26, 2015
    Posts:
    10
    Thank you #Dantus for helping me out. Sorry that I am new to this forum and it was my mistake not knowing the correct way of posting the issue in the right format. Thanks for pointing this out I will remember to do it correctly Cheers! :D
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Also post line numbers as well as the error messages
     
  8. wiredin

    wiredin

    Joined:
    Nov 26, 2015
    Posts:
    10
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class maze : MonoBehaviour {
    6.     [System.Serializable]
    7.     public class Cell {
    8.         public bool visited;
    9.         public GameObject north;//1
    10.         public GameObject east;//2
    11.         public GameObject west;//3
    12.         public GameObject south;//4
    13.  
    14.     }
    15.  
    16.     public GameObject wall;
    17.     public float wallLength = 1.0f;
    18.     public int xSize = 5;
    19.     public int ySize = 5;
    20.     private Vector3 initialPos;
    21.     private GameObject wallHolder;
    22.     private Cell[] cells;
    23.     private int currentCell = 0;
    24.     private int totalCells;
    25.     private int visitedCells = 0;
    26.     private bool startedBuilding = false;
    27.     private int currentNeighbour = 0;
    28.     private List<int> lastCells;
    29.     private int backingUp = 0;
    30.     private int wallToBreak = 0;
    31.  
    32.     // Use this for initialization
    33.     void Start () {
    34.         CreateWalls ();
    35.     }
    36.  
    37.     void CreateWalls (){
    38.         wallHolder = new GameObject ();
    39.         wallHolder.name = "Maze";
    40.  
    41.         initialPos = new Vector3 ((-xSize / 2) + wallLength / 2, 0.0f, (-ySize / 2) + wallLength / 2);
    42.         Vector3 myPos = initialPos;
    43.         GameObject tempWall;
    44.  
    45.         //For X Axis
    46.         for (int i = 0; i < ySize; i++) {
    47.             for (int j = 0; j <= xSize; j++) {
    48.                 myPos = new Vector3 (initialPos.x + (j*wallLength)-wallLength/2,0.0f,initialPos.z+(i*wallLength)-wallLength/2);
    49.                 tempWall = Instantiate(wall,myPos,Quaternion.identity) as GameObject;
    50.                 tempWall.transform.parent = wallHolder.transform;
    51.             }
    52.         }
    53.  
    54.         //For Y Axis
    55.         for (int i = 0; i <= ySize; i++) {
    56.             for (int j = 0; j < xSize; j++) {
    57.                 myPos = new Vector3 (initialPos.x + (j*wallLength),0.0f,initialPos.z+(i*wallLength)-wallLength);
    58.                 tempWall = Instantiate(wall,myPos,Quaternion.Euler(0.0f,90.0f,0.0f)) as GameObject;
    59.                 tempWall.transform.parent = wallHolder.transform;
    60.             }
    61.         }
    62.         createCells ();
    63.     }
    64.  
    65.  
    66.     void createCells () {
    67.         lastCells = new List<int> ();
    68.         lastCells.Clear ();
    69.         totalCells = xSize * ySize;
    70.         GameObject[] allWalls;
    71.         int children = wallHolder.transform.childCount;
    72.         allWalls = new GameObject[children];
    73.         cells = new Cell[xSize * ySize];
    74.         int eastWestProcess = 0;
    75.         int childProcess = 0;
    76.         int termCount = 0;
    77.  
    78.         //Get All the children
    79.         for (int i = 0; i < children; i++) {
    80.             allWalls[i] = wallHolder.transform.GetChild(i).gameObject;
    81.         }
    82.         //Assigns walls to the cells
    83.         for (int cellprocess = 0; cellprocess < cells.Length; cellprocess++) {
    84.  
    85.             if (termCount == xSize) {
    86.                 eastWestProcess ++;
    87.                 termCount = 0;
    88.             }
    89.  
    90.             cells[cellprocess] = new Cell ();
    91.             cells[cellprocess].east = allWalls[eastWestProcess];
    92.             cells[cellprocess].south = allWalls[childProcess+(xSize+1)*ySize];
    93.  
    94.             eastWestProcess++;
    95.  
    96.             termCount++;
    97.             childProcess++;
    98.             cells[cellprocess].west = allWalls[eastWestProcess];
    99.             cells[cellprocess].north = allWalls[(childProcess+(xSize+1)*ySize)+xSize-1];
    100.         }
    101.         Createmaze ();
    102.     }
    103.  
    104.     void Createmaze () {
    105.         while (visitedCells < totalCells) {
    106.             if (startedBuilding) {
    107.                 GiveMeNeighbour();
    108.                 if (cells[currentNeighbour].visited == false && (cells[currentCell].visited == true){
    109.                     BreakWall();
    110.                     cells[currentNeighbour].visited = true;
    111.                     visitedCells++;
    112.                     lastCells.Add(currentCell);
    113.                     currentCell = currentNeighbour;
    114.                     if (lastCells.Count > 0) {
    115.                         backingUp = lastCells.Count -1;
    116.                     }
    117.                 }
    118.             }
    119.             else {
    120.                 currentCell = Random.Range(0,totalCells);
    121.                 cells[currentCell].visited = true;
    122.                 visitedCells++;
    123.                 startedBuilding = true;
    124.             }
    125.  
    126.         }
    127.                 Debug.Log("Finished");
    128.     }
    129.  
    130.     void BreakWall(){
    131.         switch (wallToBreak) {
    132.         case 1 : Destory(cells[currentCell].north); break;
    133.         case 2 : Destory(cells[currentCell].east); break;
    134.         case 3 : Destory(cells[currentCell].west); break;
    135.         case 4 : Destory(cells[currentCell].south); break;
    136.  
    137.         }
    138.     }
    139.  
    140.     void GiveMeNeighbour() {
    141.         int length = 0;
    142.         int[] neighbours = new int[4];
    143.         int[] connectingWall = new int[4];
    144.         int check = 0;
    145.         check = ((currentCell + 1) / xSize);
    146.         check -= 1;
    147.         check *= xSize;
    148.         check += xSize;
    149.         //west
    150.         if (currentCell + 1 < totalCells && (currentCell + 1) != check) {
    151.             if (cells [currentCell + 1].visited == false) {
    152.                 neighbours [length] = currentCell + 1;
    153.                 connectingWall[length] = 3;
    154.                 length++;
    155.             }  
    156.         }
    157.  
    158.         //east
    159.         if (currentCell - 1 >= 0 && currentCell != check) {
    160.             if (cells [currentCell - 1].visited == false) {
    161.                 neighbours [length] = currentCell - 1;
    162.                 connectingWall[length] = 2;
    163.                 length++;
    164.             }  
    165.         }
    166.  
    167.         //north
    168.         if (currentCell + xSize < totalCells) {
    169.             if (cells [currentCell + xSize].visited == false) {
    170.                 neighbours [length] = currentCell + xSize;
    171.                 connectingWall[length] = 1;
    172.                 length++;
    173.             }  
    174.         }
    175.  
    176.  
    177.         //south
    178.         if (currentCell - xSize >= 0) {
    179.             if (cells [currentCell - xSize].visited == false) {
    180.                 neighbours [length] = currentCell - xSize;
    181.                 connectingWall[length] = 4;
    182.                 length++;
    183.             }  
    184.         }
    185.         if (length != 0) {
    186.                 int theChosenOne = Random.Range (0,length);
    187.                 currentNeighbour = neighbours[theChosenOne];
    188.                 wallToBreak = connectingWall[theChosenOne];
    189.         } else {
    190.             if (backingUp > 0) {
    191.                 currentCell = lastCells[backingUp];
    192.                 backingUp--;
    193.             }
    194.         }
    195.  
    196. }
    197.         // Update is called once per frame
    198.         void Update () {
    199.    
    200.     }
    Now I finally completed my script but then I encountered multiple errors.

    Assets/Scripts/maze.cs(108,117): error CS1525: Unexpected symbol `{'
    Assets/Scripts/maze.cs(119,28): error CS1525: Unexpected symbol `else'
    Assets/Scripts/maze.cs(127,42): error CS8025: Parsing error

    Sorry that I am still at the learning stage of C# I tried to resolve the parsing error but ended up getting more errors so in the end i decided to revert back to the original state.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You are missing a closing ) on line 108. Every ( must be paired with a ). Same goes for { } and < >.
     
  10. wiredin

    wiredin

    Joined:
    Nov 26, 2015
    Posts:
    10
    i check on every line for the opening and closing all is correct but there is still a parsing error on line (200,9)
     
  11. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    as far as a see the last code you posted is missin a closing tag for the class
     
    Kiwasi likes this.
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No its not. If it was you wouldn't get a compiler error. You need at least one extra closing brace on line 201 to close off the class scope from line 5