I need help with my c# code

I need help with my script.
Does it keeps having the error " The modifier ‘public’ is not valid for this item" could someone please help?
The script:
internal class Program
{
public static void Main(string args)
{
public void Update()
{
Rigidbody rb = GetComponent();
if (Input.GetKey(KeyCode.A))
rb.AddForce(Vector3.left);
if (Input.GetKey(KeyCode.D))
rb.AddForce(Vector3.right);
if (Input.GetKey(KeyCode.W))
rb.AddForce(Vector3.up);
if (Input.GetKey(KeyCode.S))
global::System.Object value = rb.AddForce(Vector3.down);
}
}
}

internal class Program
{
public static void Main(string args)
{

    }

    public void Update() 
    { 
        Rigidbody rb = GetComponent<Rigidbody>();

        if (Input.GetKey(KeyCode.A))
             rb.AddForce(Vector3.left); 

        if (Input.GetKey(KeyCode.D))
             rb.AddForce(Vector3.right); 

        if (Input.GetKey(KeyCode.W))
             rb.AddForce(Vector3.up);

        if (Input.GetKey(KeyCode.S))
            rb.AddForce(Vector3.down);
    }
}

This should be your fixed code.


Firstly, only if you’re programming a console project with raw C#, you will need the Main() method. Since you are programming in Unity (I assume), your scripts do not need the Main() method. If you are not programming using a Unity script, the Update() method will do nothing, and Rigidbody is not a component in the raw C# libraries.


Secondly, your code was not executing, because you defined Update() within the Main() method. Function definitions cannot exist within other function definitions. They must be separated with closed curly braces. Update() methods can have public modifiers, unlike what holbanrobertmarian said above.

Please follow any tutorials you are learning from closely, before you want to create your own projects. @TheRealDevBros

Please edit you question and insert the code using the Code button (ones and zeros). Also, the error message will tell you which one is in error. Please post that as well…

You can not make the Update function public, it is a Unity function that runs every frame automatically. That means the function is called automatically, without the need of calling it manually. In conclusion, the Update function is running every frame and is automatically called. What you did in your code does not make sense, but I assume you are a beginner.
So, the very simple fix would be to instead of “public void Update()”, type “private void Update()” or “void Update()”. They are the exact same things. In addition, if you type neither “public” nor “private” in front of your Function or in front of type reference (float, string, GameObject, Transform, things like that), your Function or type will be private every time. It is a good approach because your code will be a little cleaner at the end.
I hope it is going to help you!