Sprite2D visible switch

Godot Version

4.3

Question

Hi !
On a collision of player, I’d like to show a “GAME OVER” sprite on the screen.
Therefore, I have made a Sprite2D called “GameOver”, and removed the “visible” option in the inspector.

Then, on player script, I have this :

func _physics_process(delta):
(…)
var collision = move_and_collide(velocity*delta)
if collision:
if collision.get_collider().name == “Ennemy”:
print(“GAME OVER”)
$GameOver.visible = true

I get the “GAMEOVER” in the console but i get "invalid assignement of property or key ‘visible’ with value of type ‘bool’ on a base object of type ‘null instance’

it means it doesn’t find $GameOver ?
Any hint to show the sprite2D ?

Thanks

You need to show a print of your scene structure for help understand why the node is not being found

Your code doesn’t work because $ (alias to get_node()) will do a relative path search, that means $GameOver in Player node will search for GamerOver in their childs nodes, you need to go back for the parent node first before search the GameOver node:

get_parent().get_node("GameOver").visible = true

Or

$".."/GameOver.visible = true

2 Likes

Thanks !!!
Indeed, both of your answers are validated, Cheers !!!