Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question I have a problem with my movement script

Discussion in 'Getting Started' started by gamesmikser5, Feb 28, 2024.

  1. gamesmikser5

    gamesmikser5

    Joined:
    Feb 28, 2024
    Posts:
    2
    Hello,
    I'm starting to write scripts in C#, but I have a little problem with my code because I get this error:

    Assets\Scenes\movment2.cs(15,27): error CS1002: ; expected

    And this is my code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     public float moveSpeed = 5f;
    7.     public float jumpForce = 5f;
    8.  
    9.  
    10.     Rigidbody rb;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         rb = GetComponent global::System.Object value = Rigidbody();
    16.     }
    17.  
    18.  
    19.     void Update()
    20.     {
    21.         float moveX = Input.GetAxis("Horizontal");
    22.         float moveZ = Input.GetAxis("Vertical");
    23.  
    24.  
    25.         Vector3 movement = new Vector3(moveX, 0f, moveZ);
    26.  
    27.         rb.AddForce(movement * moveSpeed);
    28.  
    29.  
    30.         if (Input.GetKeyDown(KeyCode.Space));
    31.         {
    32.             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    33.         }
    34.     }
    35. }
    Can anyone help me solve my problem?
     
  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    808
    Line 15

    Code (CSharp):
    1. rb = GetComponent global::System.Object value = Rigidbody();
    should be

    Code (CSharp):
    1. rb = GetComponent<Rigidbody>();
     
    gamesmikser5 likes this.
  3. gamesmikser5

    gamesmikser5

    Joined:
    Feb 28, 2024
    Posts:
    2
    My script works now,
    thanks for your help
     
    flashframe likes this.