Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiple errors for player controller

Discussion in 'Scripting' started by Squiddu, Jul 18, 2020.

  1. Squiddu

    Squiddu

    Joined:
    Jan 10, 2020
    Posts:
    5
    For some reason, this gives multiple errors:

    Assets\Scripts\PlayerMovement.cs(8,6): error CS1513: } expected

    Assets\Scripts\PlayerMovement.cs(10,12): error CS1519: Invalid token '=' in class, struct, or interface member declaration

    Assets\Scripts\PlayerMovement.cs(10,39): error CS1519: Invalid token '(' in class, struct, or interface member declaration

    Assets\Scripts\PlayerMovement.cs(10,40): error CS8124: Tuple must contain at least two elements.

    Assets\Scripts\PlayerMovement.cs(24,1): error CS1022: Type or namespace definition, or end-of-file expected

    Here's the code I took from here:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     Rigidbody2D rb;
    6.  
    7.     void Start()
    8.     {
    9.         public float speed;
    10.         rb = GetComponent<Rigidbody2D>()
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         Move();
    16.     }
    17.    
    18.     void Move()
    19.     {
    20.         float x = Input.GetAxisRaw("Horizontal");
    21.         float moveBy = x * speed;
    22.         rb.velocity = new Vector2(moveBy, rb.velocity.y);
    23.     }
    24. }
    25.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Go back to the tutorial and carefully review the step where they insert the
    public float speed;
    line.

    With software engineering it has to be (pretty much) 100% correct, not "Sorta like." :) This includes punctuation, capitalization, spelling, and ordering.
     
    PraetorBlue likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Based on your error message you have a prob lem with the filename too. Unity expects the filename to match the class name. In your case you have "PlayerMovement.cs" but your class is called "PlayerController". Your script will not work properly with that mismatch. You need either:

    Filename: "PlayerMovement.cs", Class Name: "PlayerMovement"
    or
    Filename: "PlayerController.cs", Class Name: "PlayerController"
     
    mopthrow likes this.
  4. Squiddu

    Squiddu

    Joined:
    Jan 10, 2020
    Posts:
    5
    Fixed it. The main issue was the
    Code (CSharp):
    1. public float speed;
    being in the Start() void rather right up top with
    Code (CSharp):
    1. rb = GetComponent<Rigidbody2D>();
    .[
     
    Kurt-Dekker likes this.