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

Many errrors need help...

Discussion in 'Scripting' started by ImGundulf, May 23, 2020.

  1. ImGundulf

    ImGundulf

    Joined:
    May 17, 2020
    Posts:
    49
    Ik, I ask for help so much, but I'm new... eh so if you see something wrong in my script, please reply down below!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Paddle : MonoBehaviour
    6. {
    7.     [SerializeField] public float moveSpeed = 5f;
    8.  
    9.     public bool isAI;
    10.  
    11.     private Ball ball;
    12.     private BoxCollider2D col;
    13.  
    14.     private float randomYOffset;
    15.  
    16.     private Vector2 forwardDirection;
    17.     private bool firstIncoming;
    18.  
    19.     public enum Side { Left, Right }
    20.     [SerializeField] private Side side;
    21.  
    22.     [SerializeField] private float resetTime;
    23.     private bool overridePosition;
    24.  
    25.     private void Start()
    26.     {
    27.         ball = GameObject.FindGameObjectWithTag("Ball").GetComponent<Ball>();
    28.         col = GetComponent<BoxCollider2D>();
    29.  
    30.         if(side == Side.Left)
    31.             forwardDirection = Vector2.right;
    32.         else if(side == Side.Right)
    33.             forwardDirection = Vector2.left;
    34.     }
    35.  
    36.     private void Update()
    37.     {
    38.         if(!overridePosition)
    39.            MovePaddle();
    40.  
    41.  
    42.  
    43.     }
    44.  
    45.     private void MovePaddle()
    46.     {
    47.         float targetYPosition = GetNewYPosition();
    48.  
    49.         transform.position = new Vector3( transform.position.x, targetYPosition, transform.position.z);
    50.  
    51.         ClampPosition(ref targetYPosition);
    52.     }
    53.  
    54.     private void ClampPosition(ref float yPosition)
    55.     {
    56.         float minY = Camera.main.ScreenToWorldPoint(new Vector3(0, 0)).y;
    57.         float maxY = Camera.main.ScreenToWorldPoint( new Vector3(0, Screen.height)).y;
    58.  
    59.         yPosition = Mathf.Clamp(yPosition, minY, maxY);
    60.     }
    61.  
    62.     private float GetNewYPosition()
    63.     {
    64.         float result = transform.position.y;
    65.  
    66.  
    67.         if(isAI)
    68.         {
    69.             if(BallIncoming())
    70.             {
    71.                 if(firstIncoming)
    72.                 {
    73.                     print("First time incoming");
    74.                     firstIncoming = false;
    75.                     randomYOffset = GetRandomOffset();
    76.                 }
    77.                 result = Mathf.MoveTowards(transform.position.y, ball.transform.position.y + randomYOffset, moveSpeed * Time.deltaTime);
    78.             }
    79.             else
    80.             {
    81.                 firstIncoming = true;
    82.             }
    83.         }
    84.         else
    85.         {
    86.             float movement = Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime;
    87.             result = transform.position.y + movement;
    88.         }
    89.         return result;
    90.     }
    91.  
    92.     private bool BallIncoming()
    93.     {
    94.         float dotP = Vector2.Dot(ball.velocity, forwardDirection);
    95.         return dotP < 0f;
    96.     }
    97.    
    98.     private float GetRandomOffset()
    99.     {
    100.         float maxOffset = col.bounds.extents.y;
    101.         return Random.Range(-maxOffset, maxOffset);
    102.     }
    103.  
    104.     public void Reset();
    105.     {
    106.         StartCourotine(ResetRoutine());
    107.  
    108.     }
    109.  
    110.  
    111.     private IEnumerator ResetRoutine()
    112.     {
    113.         overridePosition - true;
    114.         float currentYPosition = transform.position.y;
    115.         for(float timer = 0; timer > resetTime; timer+= Time.deltaTime)
    116.         {
    117.             float targetPosition = Mathf.Lerp(startPosition, 0f, timer / resetTime);
    118.             transform.position = new Vector3(transform.position.x, targetPosition, transform.position.z);
    119.             yield return null;
    120.         }
    121.         transform.position = new Vector3(transform.position.x, 0, transform.position.z);
    122.         overridePosition = false;
    123.     }
    124. }
    125.  
    126.    
    thanks!
     
  2. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    You're gonna want to provide more information. What are you trying to do? What's going wrong? Any specific error messages you're receiving?
     
  3. ImGundulf

    ImGundulf

    Joined:
    May 17, 2020
    Posts:
    49
    eh, I can say all of them. (105,5): error CS1519: Invalid token '{' in class, struct, or interface member declaration, 106,36): error CS1001: Identifier expected, 106,37): error CS1003: Syntax error, ',' expected, (106,38): error CS8124: Tuple must contain at least two elements, 106,39): error CS1001: Identifier expected and (123,1): error CS1022: Type or namespace definition, or end-of-file expected, sorry if it's alot don't know how to fix... thanks! :)
     
  4. ImGundulf

    ImGundulf

    Joined:
    May 17, 2020
    Posts:
    49
     
  5. ImGundulf

    ImGundulf

    Joined:
    May 17, 2020
    Posts:
    49
    And I'm trying to control the Paddle, for a pong game. :D
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Generally speaking, look at the line the error tells you about and the problem it sees there. While not always spot on, this is generally a very good indicator for what's wrong. In your case, line 105 tells you that the compiler is confused by the { there. Why? Because at line 104 you put a semicolon behind the method declaration, which basically end the line there. The compiler is thus confused by the following scope (the curley brackets) you defined, which doesnt belong there.
    One more important thing: one error such as this, can often cause other errors. Now, because the compiler does not think of your scope {} as method body, it's not sure how to interprete it, thus it throws all kinds of errors.
    So start by removing the semicolon in line 104 and then see which errors remain.
     
    ImGundulf and leftshoe18 like this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220
     
  8. ImGundulf

    ImGundulf

    Joined:
    May 17, 2020
    Posts:
    49
    ok, thanks, but I have some errors left, if you know how to fix them, I would be so greatfull. So the first one is "error CS0103: The name 'StartCouroutine' does not exist in the current context" but I will see if I fix this the other errors will disappear... thanks
     
  9. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    Should be StartCoroutine.
     
  10. ImGundulf

    ImGundulf

    Joined:
    May 17, 2020
    Posts:
    49
    thank you, the most of the errors are gone now, only one left, error CS1001: Identifier expected, sorry, if I ask to much, but no need to reply...
     
  11. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You need to provide the line of the error, and since you probably made changes to the code above, an updated version of the code would be helpful as well.