[JS] I'm having troubles with my money system

So I made a working basic money system where there is the variable CurrentMoney and the variable MoneyAmount, and basically every time I walk over money, the variable CurrentMoney is increased by 50, and then the game object is deleted. Now the problem with this, is when the game object is deleted, so is the variable CurrentMoney, because the money script is located UNDER the object “money”
… Ive tried a lot to solve this and I just can’t think of what to do… Does anybody else know how I could make it so that the variables don’t get erased???

What you should do, is CurrentMoney should be attached to a character. When you hit the money, the character (what you control) should have the money added. You might even in some cases have a static class that holds a player reference which contains the money. This would be a good case if you had to remove the player for some reasons and didnt want the money attached to the player.

The best way to think about this, is your wallet holds money, not the money you pick up. You hold your wallet, so you being the player, who never goes away, should hold the wallet (and the picked up money) or the money count.

I think the problem is more how you structured things. You might be representing the plyer as the camera, but even then shoudl attach to a gameobject hat also has a script with your player settings.

The simplest solution is to add the variable to your player object and not to a money object.