My death counter won't work (noob)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CharlieChicken

So for my first game in godot I’ve been making a platformer. I have an area2d with a death script collisionshape2d as a child. The code for the script is

(sorry I can’t make the sample code feature work)

extends Area2D

var deaths = 0

func _on_body_entered(_body :PhysicsBody2D):
deaths = deaths + 1

This resets the player as I want, but what’s printed in the console is always 1, no matter how many times the player dies. What am I doing wrong?

Now I’m no professional, but try making the script a singleton (Project>Project Settings>AutoLoad)

iNeedMentalHelp | 2023-07-09 08:50

:bust_in_silhouette: Reply From: zeludtnecniv

I don’t work because you reload the entire scene with this script as well. This recreate everything including your counter go back to 0.
You need to save the counter in a singleton (in Autoload as mention by INeedMentalHelp) or change the way you reload your scene.