Home > Enterprise >  how should i arrange my code to fix error CS1022
how should i arrange my code to fix error CS1022

Time:12-20

i was using unity to learn 2d game development, and i just got started with coding and game develompment. i encountered the Error:CS1022 Type or namespace definition, or end-of-life expected here is the code i've written:

`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

{public class Player : MonoBehaviour
public Rigidbody2D rb;
public int movespeed;
    // Start is called before the first frame update
    void Start() { 
     rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update() {
        if (Input.GetKey(KeyCode.LeftArrow))
    {
        rb.velocity = new Vector2 (-movespeed, rb.velocity.y);
    }
        if (Input.GetKey(KeyCode.RightArrow))
    {
        rb.velocity = new Vector2 (movespeed, rb.velocity.y);
    }
    if (Input.GetKey(KeyCode.Space))
    {
        rb.velocity = new Vector2 (rb.velocity.x, 5);
    }
    
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
public bool onGround;

     void FixedUpdate()
     {
        onGround = Physics2D.OverlapCircle(groundCheck.position,
        groundCheckRadius,whatIsGround);
     }
}
} 

` i was trying to make a control for my game character's jumping ability so that it couldn't fly by jumping.

it says that the error is at the line (39,1). i can't figure out how to fix this error can anyone help and explain what i was doing wrong?

i didn't know what to do since i had followed exactly how the book i'm using to learn coding says, and i've tried rearanging the code but to no avail.

CodePudding user response:

As others have pointed out in the comments, there are several formatting errors in your code, such as an extra opening bracket { before the class declaration and a missing closing bracket } for the Update method.

If you're working with an IDE such as Visual Studio, there's formatting options built in (Edit > Advanced > Format Document) which will help you organise the bracket indentations; although they won't auto-resolve the errors

  • Related