Need help with making a sprite display as an UI

Godot version: 4.4.1

Hello, i need help with making my sprite that i used as a part of UI visible on the camera, i already put it in canvas layer as in the tutorials from the internet, but it just doesn’t display the sprite when i load the game for some reason, so here’s the node tree:


The SonicUI(not the hedgehog) is what i have problems with, i don’t know why it doesn’t wants to display. So here’s the screenshow of the editing screen:

So it’s the player’s code:

extends CharacterBody2D

var pitch_cooldown := 0.0
const GRAVITY = 200.0
const WALK_SPEED = 200
@export var SonicPitch = int(0)
var sonic_sound_playing = false
var coyote_time := 0.15  # Time allowed after leaving ground to still jump
var coyote_timer := 0.0

func _physics_process(delta):
	pitch_cooldown -= delta
	velocity.y += delta * GRAVITY
	
	if is_on_floor():
		coyote_timer = coyote_time
	else:
		coyote_timer -= delta	
	# Sonic Pitch Controls
	if pitch_cooldown <= 0.0:
		if Input.is_physical_key_pressed(KEY_S) and Input.is_physical_key_pressed(KEY_A) and not Input.is_key_pressed(KEY_X):
			if SonicPitch < 0:
				$Sprite2D/Sonicclickdown.play()
			if SonicPitch > 0:
				$Sprite2D/Sonicclickup.play()
			
			SonicPitch = 0
			pitch_cooldown = 1.0
		elif Input.is_physical_key_pressed(KEY_A) and SonicPitch < 5 and not Input.is_key_pressed(KEY_X):
			
			pitch_cooldown = 1.0
			for i in 32:
				$Camera2D/CanvasLayer/SonicUI.rotation_degrees -= 1
				await get_tree().create_timer(0.0001).timeout
			SonicPitch += 1
			$Sprite2D/Sonicclickup.play()
		elif Input.is_physical_key_pressed(KEY_S) and SonicPitch > -5 and not Input.is_key_pressed(KEY_X):
			pitch_cooldown = 1.0
			for i in 32:
				$Camera2D/CanvasLayer/SonicUI.rotation_degrees += 1
				await get_tree().create_timer(0.0001).timeout
			SonicPitch -= 1
			$Sprite2D/Sonicclickdown.play()
	
	# Sonic Screwdriver Sound Handling
	if Input.is_key_pressed(KEY_X) and coyote_timer > 0.0:
		if SonicPitch == 0.0:
			if not $Sprite2D/sonicsound0.is_playing():
				$Sprite2D/sonicsound0.play()
				sonic_sound_playing = true
		elif SonicPitch == 1.0:
			if not $Sprite2D/sonicsound1.is_playing():
				$Sprite2D/sonicsound1.play()
				sonic_sound_playing = true
		elif SonicPitch == 2.0:
			if not $Sprite2D/sonicsound2.is_playing():	
				$Sprite2D/sonicsound2.play()
				sonic_sound_playing = true
		elif SonicPitch == 3.0:
			if not $Sprite2D/sonicsound3.is_playing():
				$Sprite2D/sonicsound3.play()
				sonic_sound_playing = true
		elif SonicPitch == 4.0:
			if not $Sprite2D/sonicsound4.is_playing():
				$Sprite2D/sonicsound4.play()
				sonic_sound_playing = true
		elif SonicPitch == 5.0:
			if not $Sprite2D/sonicsound5.is_playing():
				$Sprite2D/sonicsound5.play()
				sonic_sound_playing = true
		elif SonicPitch == -1.0:
			if not $Sprite2D/"sonicsound-1".is_playing():
				$"Sprite2D/sonicsound-1".play()
				sonic_sound_playing = true
		elif SonicPitch == -2:
			if not $"Sprite2D/sonicsound-2".is_playing():
				$"Sprite2D/sonicsound-2".play()
				sonic_sound_playing = true
		elif SonicPitch == -3:
			if not $"Sprite2D/sonicsound-3".is_playing():
				$"Sprite2D/sonicsound-3".play()
				sonic_sound_playing = true
		elif SonicPitch == -4:
			if not $"Sprite2D/sonicsound-4".is_playing():
				$"Sprite2D/sonicsound-4".play()
				sonic_sound_playing = true
		elif SonicPitch == -5:
			if not $"Sprite2D/sonicsound-5".is_playing():
				$"Sprite2D/sonicsound-5".play()
				sonic_sound_playing = true

		# Stop movement when sonic sound is active
		velocity.x = 0
		$Sprite2D/AnimationPlayer.play("ScrewDriverhold")
	
	# Stop Sonic Sound when X is released
	elif not Input.is_key_pressed(KEY_X) and sonic_sound_playing:
		if SonicPitch == 0:
			$Sprite2D/sonicsound0.stop()
		elif SonicPitch == 1:	
			$Sprite2D/sonicsound1.stop()
		elif SonicPitch == 2:	
			$Sprite2D/sonicsound2.stop()
		elif SonicPitch == 3:	
			$Sprite2D/sonicsound3.stop()
		elif SonicPitch == 4:	
			$Sprite2D/sonicsound4.stop()
		elif SonicPitch == 5:	
			$Sprite2D/sonicsound5.stop()
		elif SonicPitch == -1:
			$"Sprite2D/sonicsound-1".stop()
		elif SonicPitch == -2:
			$"Sprite2D/sonicsound-2".stop()
		elif SonicPitch == -3:
			$"Sprite2D/sonicsound-3".stop()
		elif SonicPitch == -4:
			$"Sprite2D/sonicsound-4".stop()
		elif SonicPitch == -5:	
			$"Sprite2D/sonicsound-5".stop()
		sonic_sound_playing = false
	
	# Player Movement and Animation
	elif Input.is_action_pressed("ui_left") and Input.is_physical_key_pressed(KEY_Z) and is_on_floor():
		if coyote_timer > 0.0:
			velocity.x = -WALK_SPEED
			if $Sprite2D/AnimationPlayer.current_animation != "Jump_Animation":
				$Sprite2D/AnimationPlayer.play("Jump_Animation")
			if is_on_floor():
				velocity.y = -GRAVITY
		else:
			pass
	
	elif Input.is_action_pressed("ui_right") and Input.is_physical_key_pressed(KEY_Z) and is_on_floor():
		if coyote_timer > 0.0:
			velocity.x = WALK_SPEED
			if $Sprite2D/AnimationPlayer.current_animation != "Jump_Animation":
				$Sprite2D/AnimationPlayer.play("Jump_Animation")
			if is_on_floor():
				velocity.y = -GRAVITY
		else:
			pass
	
	elif Input.is_action_pressed("ui_right") and Input.is_action_pressed("ui_left"):
		velocity.x = 0
		$Sprite2D/AnimationPlayer.play("Idle_Animation")
	
	elif Input.is_action_pressed("ui_left"):
		velocity.x = -WALK_SPEED
		$Sprite2D/AnimationPlayer.play("Walk_Animation")
	elif Input.is_action_pressed("ui_right"):
		velocity.x =  WALK_SPEED
		$Sprite2D/AnimationPlayer.play("Walk_Animation")
	elif Input.is_key_pressed(KEY_Z):
		if coyote_timer > 0.0:
			if $Sprite2D/AnimationPlayer.current_animation != "Jump_Animation":
				$Sprite2D/AnimationPlayer.play("Jump_Animation")
			if is_on_floor():
				velocity.y = -GRAVITY
		else:
			pass
	elif not Input.is_key_pressed(KEY_Z) and $Sprite2D/AnimationPlayer.current_animation == "Jump_Animation":
		if coyote_timer > 0.0:
			if $Sprite2D/AnimationPlayer.current_animation != "Jump_Animation":
				$Sprite2D/AnimationPlayer.play("Jump_Animation")
		else:
			pass
	
	# Default Idle State
	else:
		velocity.x = 0
		$Sprite2D/AnimationPlayer.play("Idle_Animation")

	# Flip sprite direction based on velocity
	if velocity.x:
		$Sprite2D.flip_h = velocity.x < 0
		$Sprite2D/Area2D/CollisionShape2D.position.x = absf($Sprite2D/Area2D/CollisionShape2D.position.x) * signf(velocity.x)

	if not is_on_floor() and velocity.y > 0: # in air, AND dropping
		if $Sprite2D/AnimationPlayer.current_animation != "Fall_Animation":
			$Sprite2D/AnimationPlayer.play("Fall_Animation")

	# Move player
	move_and_slide()


func _on_area_2d_body_entered(body: StaticBody2D) -> void:
	if body.is_in_group("Mechanic"):
		if body.SonicPitch_req == SonicPitch:
			if body.has_method("Breaking"):
				body.Breaking()
			else:
				pass
	else:
		pass

And also i added the script to the SonicUI but it’s just extends Sprite2D so it doesn’t do anything. But i still can’t find a way on how to fix it, i need help or a code example or something idk. I’ll be glad for the help!

Try using a TextureRect instead of a Sprite2D, maybe you need to set the CanvasLayer’s layer higher too.

1 Like