- Home /
How to sum up total coins after every level for Game's Home scene.
I am using a game object with dontdestroyonload() to store the coins earned in a level and to add them to get the total coins collected after playing the game n times. Also, this game object is storing the highest score which compares and set the highest score if greater score is achieved. The high score is working fine, but the total coins is not, the coins are added twice than what is really collected. For example if the player collects 10 coins in the game, it add up 20 coins, if next time it collects 6 coins then it adds 12 coins. It shows 32 coins, but in real the player collected 16 coins only. Take a look at the script that I had used. I am new to unity. Please provide an efficient way to get the total coins collected.
Answer by Vega4Life · Jul 19, 2019 at 03:09 PM
For me, the issue looks like it's:
void Update()
{
SceneManager.activeSceneChanged += CoinsAdd; <-- bad
Every update you are adding another callback to the activeSceneChanged event... eek. So basically, there could be a thousand callbacks there, and even though you do SceneManager.activeSceneChanged -= CoinsAdd... that only removes one of them.
So this means a few get through and will add more coins, luckily its only twice because 'SwitchPauseMenu.gameOver gets set to false.
My advice is to add the callback to onAwake, then don't worry about removing it through CoinsAdd.
Thank you my friend, it worked. I added it to awake(). But a small problem still there, when i restart the scene twice, it stores the value second time, but first time it ignores to save it. So, still stuck.
Your answer
Follow this Question
Related Questions
coins go to 0 1 Answer
Text not setting to correct value 1 Answer
Is there a better script for permanently deleting items? 0 Answers
Who can help me with lock/unlock and hint/coin systems? 0 Answers
points system with coins 3 Answers