Hello im new to unity and i need some help

So i got this error: Assets\Player\Player.cs(27,14): error CS0116: A namespace cannot directly contain members such as fields or methods
And all i know about this error is that the lines of codes that are bad are 27 and 14

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

public class Player : MonoBehaviour
{
    private bool jumpKeyWasPressed;
    private float horizonalInput;
    // Start is called before the first frame update
    void Start()
    {
    
    }



    void update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jumpKeyWasPressed = true;
        }
        horizonalInput = Input.GetAxis("Horizontal");
    }
}

private void FixedUpdate()
{
    if (jumpKeyWasPressed)
    {
        GetComponent<Rigidbody>().AddForce(Vector3.up * 5, ForceMode.VelocityChange);
        jumpKeyWasPressed = false;
    }

   GetComponent<Rigidbody>().velocity = new Vector3(horizontalInput, GetComponent<Rigidbody>().velocity.y, 0);

Almost :slight_smile:

Close your FixedUpdate function with a } on line 36.

FixedUpdate is also outside of the class. This is what the error is referring to. You can’t put functions outside of a class. (27,14) refers to the 27th line and 14th character rather than two different lines.

To fix it, your class ends with the close curly brace } on line 25 . Move that down a bit, and put the whole of FixedUpdate before it.

Also, be careful of capitalisation. On line 17 update should be Update, or it won’t run unless you call it manually.

1 Like

Thanks alot and also the lower case letter u on update wasn’t the mistake on the actual code this just when i wrote it here on the post

1 Like

ALWAYS use cut and paste, never retype code.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand errors in general:

https://discussions.unity.com/t/824586/8

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

Far and away the most common and successful form of code reuse… :wink:
But I usually copy and paste; cut is kinda scorched earth… :smile:

1 Like

True, but the bug goes away. :slight_smile: