I’m completely new to script writing and I’m struggling to understand the most basic principles. I’ve been spending hours upon hours trying to implement simple scripts to make my character move in a 2D space and no matter what I do and no matter how many videos and tutorials I watch it just never works. I follow these tutorials to the letter and input everything exactly as suggested, yet it never works and I’m always greeted with a plethora of errors. The only thing I know I’m doing differently is using my own sprite work, and as far as I can tell it shouldn’t affect the script at all.
At the moment the only thing I want to do is create a script to make my character move left and right when the arrow keys are pressed. My character currently has Capsule Collider and Rigidbody2D components implemented. Below are images of what I’ve done so far, so any feedback would be greatly appreciated.
Hello! Yep, there are a bunch of errors there, let’s go one by one
Line 15: You are using “-” instead of “=”.
Line 22: The function “GetAxis” is fomr Input class, so you have to change moveInput.GetAxis with just Input.GetAxis, otherwise you are trying to acces this function from a Vector2 variable instad form Input class.
Line 23 and 29: The variable moveVelocity is not declared anywhere, and since you are trying to use it in 2 different functions, you need to declare it globally. Add “Vector2 moveVelocity;” before your Start function and it should work.
Also, speaking honestly, I don’t understand most of these terms. I think public float means it’s visible as an editable value in the inspector window? That’s about the extent of my knowledge right now. Maybe this level of script writing is too advanced for me for the moment.
It would be nice to see how your code looks now in order to finde the remaining errors.
In OOP (Object Oriented Programming) languages like C#, public means that variable is accesible from any class. For example, you can acces rigidbody.position because the Rigidbody class has a variable called “position” with the public modifier. I case it had private or protected modifier, you wouldn’t be able to access it. Also, if you want to know more about visibility modifiers, check the link below.
Beside that, Unity shows in Inspector any public variable by default. If you want to show a non public variable in Inspector, you can add the [SerializeField] attribute before the variable declaration. In the other hand, if you want to hide a public variable, you can use the [HideInInspector] modifier. Example:
public float number1;
[HideInInspector]
public float number2;
private float number3;
[SerializeField]
private float number4;
//number1 and number4 are shown in Inspector, number2 and number3 are not visible.