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. Dismiss Notice

Resolved CS1519 and CS1022 error in 2d top down game

Discussion in 'Scripting' started by SpiderPig1660, Jan 4, 2021.

  1. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    I have been looking everywhere in the code, but still don't know where the error is.
    I know it a simple code, but I'm a noobie lol

    All the errors:
    (32,5): error CS1519: Invalid token '{' in class, struct, or interface member declaration
    (33,21): error CS1519: Invalid token '=' in class, struct, or interface member declaration
    (35,1): error CS1022: Type or namespace definition, or end-of-file expected

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     public float speed;
    9.     public Rigidbody2D rb;
    10.  
    11.     private Vector2 movedirection;
    12.  
    13.     void Update()
    14.     {
    15.         Input();
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         Move();
    21.     }
    22.  
    23.     void Input()
    24.     {
    25.         float moveX = Input.GetAxisRaw("Horizontal");
    26.         float moveY = Input.GetAxisRaw("Vertical");
    27.  
    28.         movedirection = new Vector2(moveX, moveY).normalized;
    29.     }
    30.  
    31.     void Move();
    32.     {
    33.         rb.velocity = new Vector2(movedirection.x* speed, movedirection.y* speed);
    34.     }
    35. }
    36.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    On line 31, you put a semicolon after your declaration of
    void Move()
    . That makes the computer think that the declaration was over, so by the time it encountered the function body, it was looking for the next function instead.
     
    SpiderPig1660 likes this.
  3. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    tysm :D
    but now another error pop up, "(25,23): error CS0119: 'PlayerMovement.Input()' is a method, which is not valid in the given context". What does that mean?
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Usually "Input" refers to a special Unity thing. But since you defined your own thing named "Input" inside of your class, the computer assumes that's the one you're talking about (when inside of that class).

    Either change your "Input" function to have a different name, or else spell out the full name "UnityEngine.Input" when you want the Unity version.
     
    SpiderPig1660 likes this.
  5. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    Ty for the clear explanation, I changed Input() to Inputs() and now there no error showing up, but when I click play, the character go in the direction top left when I didn't even press any button. (sorry for asking too many questions)
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Might be time to start adding Debug.Log statements to narrow down exactly when things go wrong.

    Could be that your input device is sending a signal that isn't 100% exactly zero. Since you normalize the input vector, even a tiny input will produce the same movement as maximum input. That still shouldn't happen with keyboards--I don't think--but if you've got a gamepad with an analog stick plugged in, I wouldn't be surprised if that's causing issues.

    Could be that something is going wrong with the normalization itself. Unity documentation says that if the vector is "too small to normalize" then a zero vector should be returned, so normalizing zero should give you zero, but I've never personally tested that, that I can recall.

    Could be that the player is being moved by something other than this script.
     
    SpiderPig1660 likes this.
  7. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    Nvm, I changed the skip and I use the new input system in unity, so it work now :D