Language

not resetting the death count on reload

I have a death counter that each 3 deaths the death resets to 0 and a revmob ad is displayed. but when i reset the level the death count also resets, i tried making it static but it keeps counting after 3 and doesn't resets.
I thought using PlayerPref but I'm not sure how efficient that is and I didn't test it, can somebody shed a light upon my problem?

5 Replies

The solution is to make it static, if you say that it didn't reset when you tried it, you should force the reset in any start() method (or in your resetLevel() method, if it exists).
I suggest you make a static class called GlobalVariables and inside this class put the public static int numOfDeaths. Then you could access it from any script this way:

My game starts, play play play, character dies, death count = 1
restart the game, play play play, character die, death count = 1.
its resets itself as the scene does too, i want the death count to stay 1 even after it reloaded, and be 2 when died, at 3 deaths display the ad and make the variable count from 0, endless loop.

When you say "restart the game", do you mean: "reload the scene"?
If the answer is yes, the easiest way is what I said in my previous answer. If you don't like it, or you can't make it work, explain us what is happening and we'll try another way.

The problem is that the variable is private from the class. It is created again when the class is recreated, so it will always have the same value. You need something like a gamestate singleton that survives your reloads of the levels. Here is a nice article showing you how to implement it:

it keeps counting after 3 if i make it static, "i tried making it static but it keeps counting after 3 and doesn't resets"
I'm looking for a way it'll count to 3 show ad and reset the count, static makes it count after 3 and reloading the scene resets the variable death back to 2

yes, it is public.
I thought about maybe attaching the death count to a gameobject in the scene and using DontDestroyOnLoad method, but that seems to me like a waste of resources and making things messy.