Line 12:Unexpected "if" in class body.

Godot Version

Godot 4.6.1

Question

Im really new to godot and im trying to get a gun to work but my script says Line 12:Unexpected “if” in class body. (which i dont know how to fix)

(here is my script)

extends RigidBody3D

var mouse_senstivity :=0.001
var twist_input :=0.0
var pitch_input := 0.0

@onready var twist_pivot := $TwistPivot
@onready var pitch_pivot := $TwistPivot/PitchPivot
@onready var gun_anim := $TwistPivot/PitchPivot/Camera3D/Gun/RootNode/Shotgun_SawedOff/AnimationPlayer

#shooting
if Input.is_action_pressed(“shoot”): (HERE IS WHERE THE ERROR IS)!!!

func _ready() → void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _process(delta: float) → void:
var input := Vector3.ZERO
input.x = Input.get_axis(“move_left”, “move_right”)
input.z = Input.get_axis(“move_foward”, “move_back”)

apply_central_force(twist_pivot.basis * input * 1200 * delta)

if Input.is_action_pressed("ui_cancel"):
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	
twist_pivot.rotate_y(twist_input)
pitch_pivot.rotate_x(pitch_input)
pitch_pivot.rotation.x = clamp(pitch_pivot.rotation.x,
deg_to_rad(-30),
deg_to_rad(30)
)
twist_input = 0.0
pitch_input = 0.0

func _unhandled_input(event: InputEvent) → void:
if event is InputEventMouseMotion:
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
twist_input = - event.relative.x * mouse_senstivity
pitch_input = - event.relative.y * mouse_senstivity

You can’t have an if statement outside of the function. This line makes no sense on its own anyway, what were you trying to achieve with it?

Can I suggest you maybe try this tutorial series to learn the fundamentals of Godot and GDScript?

3 Likes

In the future, please make sure to use the code feature for your text, without it we cannot read your code properly

3 Likes

GDscript doesn’t run like lua or python.

Every script is a class definition, meaning you cannot write code you expect would execute as is. Every instruction you want to execute should be put in a dedicated block.

You can think of functions, property defintions, …

E.g

func my_function():
  print("This is an instruction!")

Or for properties

var my_property: int = a:
  get:
    print("Getter block !")
    return 3 * my_property
  set(value):
    print("Setter block !")
    my_property = value / 2

There are also variable assignations within definitions which support short expressions !

var my_var: int = compute_int()
var my_other_int = 3 * my_int

Either way, aside from those main 3 cases (and if I did not forget any), you can’t write instructions anywhere else

I suggest you to read the following documentation page which is very useful : GDScript — Godot Engine (stable) documentation in English

2 Likes

Format your code like this:

```gd
#paste your code here
```

It’ll then look like this:

#paste your code here

Also, you have two problems with that line of code. Line 12 needs to be inside your _process() function, and you have to have it do something after that on the next line. (That error is hidden right now because of the other one.)

I recommend you do this tutorial and learn the basics of using Godot before going any farther: Your first 2D game — Godot Engine (stable) documentation in English

Looks like @wchc gave you the same advice.

1 Like