• 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 /
  • Help Room /
avatar image
0
Question by kaylafoster11 · May 06, 2016 at 03:49 PM · scripting problemscript errorboolunexpected-symboltetris

unexpected symbol

Im trying to build a tetris game however the script will not work. it keeps saying unexpected symbol "bool" on line 18 and i do not know how to fix it.

 using UnityEngine;
 using System.Collections;
 
 public class Grid : MonoBehaviour {
     // The Grid itself
     public static int w = 10;
     public static int h = 20;
     public static Transform[,] grid = new Transform[w, h];
 }
 
 public class Vector2 : MonoBehaviour 
 {
 public static Vector2 roundVec2(Vector2 v) {
     return new Vector2(Mathf.Round(v.x),
         Mathf.Round(v.y));
     }
 }
 public static bool insideBorder(Vector2 pos)
     {
     return ((int)pos.x >= 0 &&
         (int)pos.x < w &&
         (int)pos.y >= 0);
 }
 public static void deleteRow(int y) {
     for (int x = 0; x < w; ++x) {
         Destroy(grid[x, y].gameObject);
         grid[x, y] = null;
     }
 }
 public static void decreaseRow(int y) {
     for (int x = 0; x < w; ++x) {
         if (grid[x, y] != null) {
             // Move one towards bottom
             grid[x, y-1] = grid[x, y];
             grid[x, y] = null;
 
             // Update Block position
             grid[x, y-1].position += new Vector3(0, -1, 0);
         }
     }
 }
 public static void deleteFullRows() {
     for (int y = 0; y < h; ++y) {
         if (isRowFull(y)) {
             deleteRow(y);
             decreaseRowsAbove(y+1);
             --y;
         }
     }
 }
 bool isValidGridPos() {        
     foreach (Transform child in transform) {
         Vector2 v = Grid.roundVec2(child.position);
 
         // Not inside Border?
         if (!Grid.insideBorder(v))
             return false;
 
         // Block in grid cell (and not part of same group)?
         if (Grid.grid[(int)v.x, (int)v.y] != null &&
             Grid.grid[(int)v.x, (int)v.y].parent != transform)
             return false;
     }
     return true;
 }
 oid updateGrid() {
     // Remove old children from grid
     for (int y = 0; y < Grid.h; ++y)
         for (int x = 0; x < Grid.w; ++x)
             if (Grid.grid[x, y] != null)
             if (Grid.grid[x, y].parent == transform)
                 Grid.grid[x, y] = null;
 
     // Add new children to grid
     foreach (Transform child in transform) {
         Vector2 v = Grid.roundVec2(child.position);
         Grid.grid[(int)v.x, (int)v.y] = child;
     }        
 }
 void Update() {
     // Move Left
     if (Input.GetKeyDown(KeyCode.LeftArrow)) {
         // Modify position
         transform.position += new Vector3(-1, 0, 0);
 
         // See if valid
         if (isValidGridPos())
             // Its valid. Update grid.
             updateGrid();
         else
             // Its not valid. revert.
             transform.position += new Vector3(1, 0, 0);
     }
 }
 else if (Input.GetKeyDown(KeyCode.RightArrow)) {
     // Modify position
     transform.position += new Vector3(1, 0, 0);
 
     // See if valid
     if (isValidGridPos())
         // It's valid. Update grid.
         updateGrid();
     else
         // It's not valid. revert.
         transform.position += new Vector3(-1, 0, 0);
 }
 else if (Input.GetKeyDown(KeyCode.UpArrow)) {
     transform.Rotate(0, 0, -90);
 
     // See if valid
     if (isValidGridPos())
         // It's valid. Update grid.
         updateGrid();
     else
         // It's not valid. revert.
         transform.Rotate(0, 0, 90);
 }
 else if (Input.GetKeyDown(KeyCode.DownArrow)) {
     // Modify position
     transform.position += new Vector3(0, -1, 0);
 
     // See if valid
     if (isValidGridPos()) {
         // It's valid. Update grid.
         updateGrid();
     } else {
         // It's not valid. revert.
         transform.position += new Vector3(0, 1, 0);
 
         // Clear filled horizontal lines
         Grid.deleteFullRows();
 
         // Spawn next Group
         FindObjectOfType<Spawner>().spawnNext();
 
         // Disable script
         enabled = false;
     }
 }
 float lastFall = 0;
 else if (Input.GetKeyDown(KeyCode.DownArrow) ||
     Time.time - lastFall >= 1) {
     // Modify position
     transform.position += new Vector3(0, -1, 0);
 
     // See if valid
     if (isValidGridPos()) {
         // It's valid. Update grid.
         updateGrid();
     } else {
         // It's not valid. revert.
         transform.position += new Vector3(0, 1, 0);
 
         // Clear filled horizontal lines
         Grid.deleteFullRows();
 
         // Spawn next Group
         FindObjectOfType<Spawner>().spawnNext();
 
         // Disable script
         enabled = false;
     }
 
     lastFall = Time.time;
 }
 void Update() {
     // Move Left
     if (Input.GetKeyDown(KeyCode.LeftArrow)) {
         // Modify position
         transform.position += new Vector3(-1, 0, 0);
 
         // See if valid
         if (isValidGridPos())
             // It's valid. Update grid.
             updateGrid();
         else
             // It's not valid. revert.
             transform.position += new Vector3(1, 0, 0);
     }
 
     // Move Right
     else if (Input.GetKeyDown(KeyCode.RightArrow)) {
         // Modify position
         transform.position += new Vector3(1, 0, 0);
 
         // See if valid
         if (isValidGridPos())
             // It's valid. Update grid.
             updateGrid();
         else
             // It's not valid. revert.
             transform.position += new Vector3(-1, 0, 0);
     }
 
     // Rotate
     else if (Input.GetKeyDown(KeyCode.UpArrow)) {
         transform.Rotate(0, 0, -90);
 
         // See if valid
         if (isValidGridPos())
             // It's valid. Update grid.
             updateGrid();
         else
             // It's not valid. revert.
             transform.Rotate(0, 0, 90);
     }
 
     // Move Downwards and Fall
     else if (Input.GetKeyDown(KeyCode.DownArrow) ||
         Time.time - lastFall >= 1) {
         // Modify position
         transform.position += new Vector3(0, -1, 0);
         void Start() {
             // Default position not valid? Then it's game over
             if (!isValidGridPos()) {
                 Debug.Log("GAME OVER");
                 Destroy(gameObject);
             }
         }
 
 
         // See if valid
         if (isValidGridPos()) {
             // It's valid. Update grid.
             updateGrid();
         } else {
             // It's not valid. revert.
             transform.position += new Vector3(0, 1, 0);
 
             // Clear filled horizontal lines
             Grid.deleteFullRows();
 
             // Spawn next Group
             FindObjectOfType<Spawner>().spawnNext();
 
             // Disable script
             enabled = false;
         }
 
         lastFall = Time.time;
     }
 }


Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jessespike · May 06, 2016 at 06:45 PM

The error happens because the functions are not inside a class. There's also a few other problems:

  • There are two Update functions doing the same thing.

  • There are two functions that don't seem to exist (isRowFull and decreaseRowsAbove)

  • The nested Vector2 class doesn't seem right. I'm not sure why this would be here.

  • The "v" missing from void on updateGrid()

  • The lastFall variable is wedged between a "}" and "else if"

Not sure what tutorial or asset this script is from, so my changes might not be compatible, but it might help. There still more work to do, such as implementing isRowFull and decreaseRowsAbove.

 using UnityEngine;
 using System.Collections;
 
 public class Grid : MonoBehaviour {
     // The Grid itself
     public static int w = 10;
     public static int h = 20;
     public static Transform[,] grid = new Transform[w, h];
 
     public static Vector2 roundVec2(Vector2 v) {
         return new Vector2(Mathf.Round(v.x),
                            Mathf.Round(v.y));
     }
 
     public static bool isRowFull(int v)    {
         // TODO Implement code
         return false;
     }
     public static void decreaseRowsAbove(int v) {
         // TODO Implement code
     }

     public static bool insideBorder(Vector2 pos)
     {
         return ((int)pos.x >= 0 &&
                 (int)pos.x < w &&
                 (int)pos.y >= 0);
     }
     public static void deleteRow(int y) {
         for (int x = 0; x < w; ++x) {
             Destroy(grid[x, y].gameObject);
             grid[x, y] = null;
         }
     }
     public static void decreaseRow(int y) {
         for (int x = 0; x < w; ++x) {
             if (grid[x, y] != null) {
                 // Move one towards bottom
                 grid[x, y-1] = grid[x, y];
                 grid[x, y] = null;
                 
                 // Update Block position
                 grid[x, y-1].position += new Vector3(0, -1, 0);
             }
         }
     }
     public static void deleteFullRows() {
         for (int y = 0; y < h; ++y) {
             if (isRowFull(y)) {
                 deleteRow(y);
                 decreaseRowsAbove(y+1);
                 --y;
             }
         }
     }
     bool isValidGridPos() {        
         foreach (Transform child in transform) {
             Vector2 v = Grid.roundVec2(child.position);
             
             // Not inside Border?
             if (!Grid.insideBorder(v))
                 return false;
             
             // Block in grid cell (and not part of same group)?
             if (Grid.grid[(int)v.x, (int)v.y] != null &&
                 Grid.grid[(int)v.x, (int)v.y].parent != transform)
                 return false;
         }
         return true;
     }
 
     void updateGrid() {
         // Remove old children from grid
         for (int y = 0; y < Grid.h; ++y)
             for (int x = 0; x < Grid.w; ++x)
                 if (Grid.grid[x, y] != null)
                     if (Grid.grid[x, y].parent == transform)
                         Grid.grid[x, y] = null;
         
         // Add new children to grid
         foreach (Transform child in transform) {
             Vector2 v = Grid.roundVec2(child.position);
             Grid.grid[(int)v.x, (int)v.y] = child;
         }        
     }
     void Update() {
         float lastFall = 0;
 
         // Move Left
         if (Input.GetKeyDown(KeyCode.LeftArrow)) {
             // Modify position
             transform.position += new Vector3(-1, 0, 0);
             
             // See if valid
             if (isValidGridPos())
                 // Its valid. Update grid.
                 updateGrid();
             else
                 // Its not valid. revert.
                 transform.position += new Vector3(1, 0, 0);
         }
         else if (Input.GetKeyDown(KeyCode.RightArrow)) {
             // Modify position
             transform.position += new Vector3(1, 0, 0);
             
             // See if valid
             if (isValidGridPos())
                 // It's valid. Update grid.
                 updateGrid();
             else
                 // It's not valid. revert.
                 transform.position += new Vector3(-1, 0, 0);
         }
         else if (Input.GetKeyDown(KeyCode.UpArrow)) {
             transform.Rotate(0, 0, -90);
             
             // See if valid
             if (isValidGridPos())
                 // It's valid. Update grid.
                 updateGrid();
             else
                 // It's not valid. revert.
                 transform.Rotate(0, 0, 90);
         }
         else if (Input.GetKeyDown(KeyCode.DownArrow)) {
             // Modify position
             transform.position += new Vector3(0, -1, 0);
             
             // See if valid
             if (isValidGridPos()) {
                 // It's valid. Update grid.
                 updateGrid();
             } else {
                 // It's not valid. revert.
                 transform.position += new Vector3(0, 1, 0);
                 
                 // Clear filled horizontal lines
                 Grid.deleteFullRows();
                 
                 // Spawn next Group
                 FindObjectOfType<Spawner>().spawnNext();
                 
                 // Disable script
                 enabled = false;
             }
         }
         else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - lastFall >= 1) {
             // Modify position
             transform.position += new Vector3(0, -1, 0);
             
             // See if valid
             if (isValidGridPos()) {
                 // It's valid. Update grid.
                 updateGrid();
             } else {
                 // It's not valid. revert.
                 transform.position += new Vector3(0, 1, 0);
                 
                 // Clear filled horizontal lines
                 Grid.deleteFullRows();
                 
                 // Spawn next Group
                 FindObjectOfType<Spawner>().spawnNext();
                 
                 // Disable script
                 enabled = false;
             }
             
             lastFall = Time.time;
         }
     }
 
 }
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unexpected Symbol ' var' 1 Answer

2D ROGUELIKE SCRIPTING HELP , 2D Roguelike Script issues 1 Answer

Health Bar Problem 1 Answer

Void cannot be used in this context, unexpected symbol expecting `,', `;', 0 Answers

Can someone please help me find out what wrong with my code. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges