Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need help with this code

Discussion in 'Scripting' started by bladeofdawn, Mar 29, 2019.

  1. bladeofdawn

    bladeofdawn

    Joined:
    Mar 29, 2019
    Posts:
    3
    It telling me unexpectedly end of line error I can’t figure out how to fix it help is much appreciate I’m doing this for a progamming class for my final



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Dot : MonoBehaviour {
    6.  
    7.     public int column;
    8.     public int row;
    9.     public int targetX;
    10.     public int targetY;
    11.     private Board board;
    12.     private GameObject otherDot;
    13.     private Vector2 firstTouchPosition;
    14.     private Vector2 finalTouchPosition;
    15.     public float swipeAngle = 0;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         board = FindObjectOfType<Board>();
    20.         targetX = (int)transform.position.x;
    21.         targetY = (int)transform.position.y;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update () {
    26.         targetX = column;
    27.         targetY = row;
    28.     }
    29.  
    30.     private void OnMouseDown()
    31.     {
    32.         firstTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    33.         //Debug.Log(firstTouchPosition);
    34.  
    35.     }
    36.  
    37.     private void OnMouseUp()
    38.     {
    39.         finalTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    40.         CalculateAngle();
    41.     }
    42.  
    43.     void CalculateAngle(){
    44.         swipeAngle = Mathf.Atan2 (finalTouchPosition.y - firstTouchPosition.y, finalTouchPosition.x - firstTouchPosition.x) * 180/ Mathf.PI;
    45.         //Debug.Log(swipeAngle);
    46.     }
    47.  
    48.     void MovePieces(){
    49.         if (swipeAngle > -45 && swipeAngle <= 45 && column < board.width){
    50.             //Right Swipe
    51.             otherDot = board.allDots [column + 1, row];
    52.             otherDot.GetComponent<Dot> ().column -= 1;
    53.             column += 1;
    54.         } else if (swipeAngle > 45 && swipeAngle <= 135 && row < Board.height){
    55.             //UP swipe
    56.             otherDot = board.allDots [column, row + 1];
    57.             otherDot.GetComponent<Dot> ().row -= 1;
    58.             row += 1;
    59.         } else if ((swipeAngle > 135 || swipeAngle <= -135) && column > 0){
    60.             //Left Swipe
    61.             otherDot = board.allDots [column - 1, row];
    62.             otherDot.GetComponent<Dot> ().column += 1;
    63.             column -= 1;
    64.         } else if(swipeAngle < -45 && swipeAngle >= -135 && row > 0){
    65.             //Down Swipe
    66.             otherDot = board.allDots [column, row - 1];
    67.             otherDot.GetComponent<Dot> ().row += 1;
    68.             row -= 1;
    69.         }
    70.     }
    71.  
     
    Last edited: Mar 29, 2019
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Use code tags.
     
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
  4. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    you need one more curly bracket at the end closing the class. The last two curly braces you have currently only close the conditional and close the method, you are missing another one to close the class.
     
    Ryiah likes this.
  5. bladeofdawn

    bladeofdawn

    Joined:
    Mar 29, 2019
    Posts:
    3
    I believe I have fixed it to where you can now read it right I’m sorry this is the first post I have made
     
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    all i found was the missing } to close the class. like Shaderbytes said
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Dot : MonoBehaviour {
    5.     public int column;
    6.     public int row;
    7.     public int targetX;
    8.     public int targetY;
    9.     private Board board;
    10.     private GameObject otherDot;
    11.     private Vector2 firstTouchPosition;
    12.     private Vector2 finalTouchPosition;
    13.     public float swipeAngle = 0;
    14.     // Use this for initialization
    15.     void Start ()
    16.     {
    17.         board = FindObjectOfType<Board>();
    18.         targetX = (int)transform.position.x;
    19.         targetY = (int)transform.position.y;
    20.     }
    21.     // Update is called once per frame
    22.     void Update ()
    23.     {
    24.         targetX = column;
    25.         targetY = row;
    26.     }
    27.     private void OnMouseDown()
    28.     {
    29.         firstTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    30.         //Debug.Log(firstTouchPosition);
    31.     }
    32.     private void OnMouseUp()
    33.     {
    34.         finalTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    35.         CalculateAngle();
    36.     }
    37.     void CalculateAngle()
    38.     {
    39.         swipeAngle = Mathf.Atan2 (finalTouchPosition.y - firstTouchPosition.y, finalTouchPosition.x - firstTouchPosition.x) * 180/ Mathf.PI;
    40.         //Debug.Log(swipeAngle);
    41.     }
    42.     void MovePieces(){
    43.         if (swipeAngle > -45 && swipeAngle <= 45 && column < board.width){
    44.             //Right Swipe
    45.             otherDot = board.allDots [column + 1, row];
    46.             otherDot.GetComponent<Dot> ().column -= 1;
    47.             column += 1;
    48.         } else if (swipeAngle > 45 && swipeAngle <= 135 && row < Board.height){
    49.             //UP swipe
    50.             otherDot = board.allDots [column, row + 1];
    51.             otherDot.GetComponent<Dot> ().row -= 1;
    52.             row += 1;
    53.         } else if ((swipeAngle > 135 || swipeAngle <= -135) && column > 0){
    54.             //Left Swipe
    55.             otherDot = board.allDots [column - 1, row];
    56.             otherDot.GetComponent<Dot> ().column += 1;
    57.             column -= 1;
    58.         } else if(swipeAngle < -45 && swipeAngle >= -135 && row > 0){
    59.             //Down Swipe
    60.             otherDot = board.allDots [column, row - 1];
    61.             otherDot.GetComponent<Dot> ().row += 1;
    62.             row -= 1;
    63.         }
    64.     }
    65. }
     
  7. PureRockets

    PureRockets

    Joined:
    Nov 16, 2016
    Posts:
    4
    This code is correct. Except you look to be using both board.width and Board.height, it should be lower case in your code. And I trust that you have a Board class elsewhere in your namespace.
     
  8. bladeofdawn

    bladeofdawn

    Joined:
    Mar 29, 2019
    Posts:
    3
    Ah! Thank you I will definitely try that I didn’t think of that
     
  9. PureRockets

    PureRockets

    Joined:
    Nov 16, 2016
    Posts:
    4
    Yes, you will need to try that because it would not compile otherwise! :)