【解決方法】CS8803: 最上位のステートメントは名前空間と型の宣言の前に置く必要があります


これを解決するにはどうすればよいですか。assets\scripts\playercontroller.cs(13, 1): error CS8803: トップレベルのステートメントは名前空間と型の宣言よりも前に置く必要があります。

私が試したこと:

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour

{
    Rigidbody rb;
    public float jumpForce;
    bool canJump;
}
    
void Awake();
    {
        rb = GetComponent();
    }

    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0) && canJump)
        {
            //jump

            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            
        }

    }

    void OnCollisionEnter(Collision collision)
    {
            if(collision.gameObject.tag == "Ground")
        {
            canJump = true;
        }
    }

    void OnCollisionExit(Collision collision)
    {
        if(collision.gameObject.tag == "Ground")
        {
            canJump = false;
        }

}

解決策 1

クラス メソッドを含める前にクラス定義を終了しました。

C#
public class PlayerController : MonoBehaviour

{
Rigidbody rb;
public float jumpForce;
bool canJump;
// } ***** remove this closing brace so the remaining code is part of the class.

また、「試したこと」セクションにある「何もない」という繰り返しのエントリをすべて削除してください。

コメント

タイトルとURLをコピーしました