im new, for some reason sprite isnt flipping

Godot Version

4.6

Question

im just trying to learn how to make top down movement thru a youtube tutorial and my sprite wont flip, this is the main error i get “Invalid assignment of property or key ‘flip_h’ with value of type ‘bool’ on a base object of type ‘null instance’” and also that “%sprite isn’t found (relative to “root/idek/player”) could this be a problem outside of my script? sry if i did anything wrong its my first time on this forum

extends CharacterBody2D

@export var movement_speed : float = 500
var character_direction : Vector2

func _physics_process(_delta):
	character_direction.x = Input.get_axis("moveleft", "moveright")
	character_direction.y = Input.get_axis("moveup", "movedown")
	character_direction = character_direction.normalized()
	
	#flip
	if character_direction.x > 0:
		%sprite.flip_h = false
	elif character_direction.x < 0:
		%sprite.flip_h = true
	
	if character_direction:
		velocity = character_direction * movement_speed
	else:
		velocity = velocity.move_toward(Vector2.ZERO, movement_speed)
	move_and_slide()

TL;DR

Make sure your sprite node is named "sprite”, and that it is marked as unique.

To mark a node as unique, go in the scene tree, right click on the desired node, and you’ll be able to mark it as unique

That means you did not mark the Sprite2D instance as unique in your scene.

In GDScript, you can write

$Name : shortcut for get_node(“Name") which retrieves a direct child of the node the script is attached to, whose name matches the param (here, "Name”).

%Name : shortcut for get_node(“%Name") which retrieves a node matching the name " Name”, in the same scene containing the scripted object. The queried node must be marque as unique (" %” symbol) within the scene. That means you must go in the editor, in the scene containing scripted node, and make sure that the queried node…

  1. Matches the name ("sprite”)
  2. Is marked as unique (has a “%" icon next to it)

Once you’ve done that, the script should retrieve it successfully, as long as your code runs during/after node readiness !

PS : uniqueness in a scene only works at the edition-level, not during runtime. Instantiating a scene and then trying to access a unique node it contains from an instance that does not belong to that scene is impossible