Search Unity

error CS1002 ; expected

Discussion in 'Getting Started' started by sashankhota, Jan 19, 2020.

  1. sashankhota

    sashankhota

    Joined:
    Jan 15, 2020
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float speed;
    8.    
    9.     // Start is called before the first frame update
    10.     void Start()
    11.  
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.  
    16.  
    17.     void FixedUpdate()
    18.     {
    19.         float moveHorizontal = Input.GetAxis("Horizontal");
    20.         float moveVertical = Input.GetAxis("Vertical");
    21.        
    22.         Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
    23.        
    24.         GetComponent<Rigidbody>().AddForce(movement*speed*Time.deltaTime);
    25.     }
    26. }
    27.  
    Help!
    Total Newbie here
    Newbie to the programming too.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, you've done well in posting your code with the proper code tags. A lot of newbies fail at that. So you're already ahead of the game there.

    Next thing to remember: when posting an error message, copy & paste the entire error message, including the file name and line number. In fact that's kind of the key part in most cases; it will usually tell you right where the problem is.

    In this case, the problem is the Update method on line 14, which you have started but not finished. Every method needs (1) a return type, or void; (2) a name, which is Update in this case; (3) a parameter list in parentheses, or empty parentheses if no parameters are needed; (4) curly braces which surround the code of the method.

    Your Update method has 1, 2, and 3, but is missing 4. So, either add some empty curly braces after Update(), or just delete lines 13-14 entirely if you don't need an Update method.
     
    sashankhota, Joe-Censored and Ryiah like this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,187
    And the Start() method on line 10. It either needs to be fully defined (return type, method name, parentheses after the method name, and curly braces after the parentheses are all required for the method definition) or it needs to be fully removed from the script. Below are examples of what it can look like defined but without any code in it.

    Code (csharp):
    1. void Start() {}
    Code (csharp):
    1. void Start() {
    2. }
    Code (csharp):
    1. void Start()
    2. {}
     
    Last edited: Jan 20, 2020
  4. sashankhota

    sashankhota

    Joined:
    Jan 15, 2020
    Posts:
    4
    Thanks to both of your reply. It worked and motivated me to keep going. Thank you so much. As I don't know anything about coding or programming I want to ask for sources to learn basics for it. Like the 4 rules, both of you have pointed out, I didn't know anything about that. So thanks again. xD
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,187
    sashankhota and JoeStrout like this.
  7. sashankhota

    sashankhota

    Joined:
    Jan 15, 2020
    Posts:
    4
  8. sashankhota

    sashankhota

    Joined:
    Jan 15, 2020
    Posts:
    4
    thanks again.