Error with my jump

Godot Version

4.6

Question

So I have been trying to make a jump mechanic, using a stripped movement script I had made along a tutorial for a game and this error pops up when I press space to jump, crashing the game in the process:

Invalid assignment of property or key ‘y’ with value of type ‘Nil’ on a base object of type ‘Vector2’.

I am baffled because it worked the day before but now it has a problem with my script.

func _physics_process(delta):

	# Handle jump.
	if Input.is_action_just_pressed("Jump"): 
		if is_on_floor():
			velocity.y = jump_force

Are you sure you assigned a float value to jump_force? From the error it seems that you declared the variable but never assigned a value to it.

This is how how jump_force looks:

@export var jump_force = -850.0

What value has jump_force in the Inspector?

Same as in the code:

If you open the .tscn file in a text editor, is there a line starting with jump_force = in it?

Are you using jump_force anywhere else in the code?

extends CharacterBody2D

@export var jump_force = -850.0
@export_range(0, 1) var decelerate_on_jump_release = 0.3


#@onready var jump_buffer_timer = $JumpBufferTimer
var jump_buffered = false





func _physics_process(delta):

	# Handle jump.
	if Input.is_action_just_pressed("Jump"): 
		if is_on_floor():
			velocity.y = jump_force
		#sprite_2d.scale = Vector2(0, 0.5)
		
		# Add the gravity
	if not is_on_floor():
		velocity += get_gravity() * delta
		
		

	#starts falling from jump
	if Input.is_action_just_released("Jump") and velocity.y < 0:
		velocity.y *= decelerate_on_jump_release


	var was_on_floor = is_on_floor()
	
	move_and_slide()


		#Touched ground
	if !was_on_floor && is_on_floor():
		if jump_buffered:
			jump_buffered = false
			print("buffered jump")

Here is the entire script. And no, I’m just using Jump force for this one bit.

And how about the .tscn file?

I’m almost a 100% sure that you either have another piece of code running that fails and not this one, OR this is a cache issue. In which case you should open up your project’s folder, find the hidden .godot folder and delete it, so the next time you open up your project, godot will re-generate the cache. (Make sure you close Godot before you delete the folder, then open it again)

You can probably tell that I am new to this lol. You want me to open the script in an external text editor?

Yes, any text editor will do. (You can do it inside Godot as well, in the script editor File > Open and then change the filter to “All Files” for the .tscn to show up.)

I tried to do what tibabersus said and I have now broken the entire game lol! Can’t open it anymore. Don’t matter, I had not gotten far into the project, I’ll just remake it. Sorry to waste your time.

WAIT, I got it working! I deleted the file and then just put it right back and now the code works!

I’m not sure how that’s possible? I assume you deleted your entire project, and NOT just the .godot folder. Do not blame me for your mistake of not reading instructions properly.

2 Likes

Wasn’t even going to blame you for my mistake, don’t worry! You were right, I deleted the file, but then I just ctrl+z in the folder, putting it right back and bam! it works for some reason now. Thank you very much!

1 Like

I got it working after doing what tibaverus said. It was a cache issue after all.

4 Likes