- Home /
Death Counter Resets When Reloading Scene.
I've tried a number of things here, but I can't seem to get the variables to stop resetting. I tried changing the integer to a static int, but it still reset. I also read that static variables should be avoided in most instances.
The script is attached to the player, and the death counter code I have is as follows:
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Dead")
{
Application.LoadLevel (Application.loadedLevel);
DeathCount++;
SetDeathText ();
}
}
If it helps in narrowing down the issue, as a test I also set the death counter to increase after a key is pushed, and the level to reset after a different one is pushed. The variables reset regardless.
Thank you.
Please post all relevant code. In this case, anything doing anything to DeathCount
should be posted. As it stands this question remains unanswerable.
That's pretty much it. Touch object, increase death count, reload.
I left this part out mistakenly:
void SetDeathText()
{
deathText.text = "Deaths: " + DeathCount.ToString();
}
Firstly: Don't post comments as answers.
Secondly: So you never declare your variable?
The only question you answer in the answers section is THE question, not just any question a passer by asked. You're probably setting your variable from somewhere else if the static keyword didn't solve your problem, that's why I asked you to post all relevant code, including the variable declaration.
Now would you kindly drop the attitude so I won't have to forcibly remove your comments.
Firstly: Get off your high horse and quit being a diva. $$anonymous$$y comment was harmless, I just pointed out that you didn't read my post when you responded to me so condescendingly and pompously.
Secondly: You're not here to help. You're here for an ego-stroke and on a power trip because you're such a big-shot who can forcibly delete posts.
Thirdly: I've got my answer, so go ahead and delete this comment too so everyone knows you're a sensitive little diva that can't handle a bit of sarcasm.
Answer by MadJohny · Feb 16, 2014 at 12:57 PM
Put this on the deathcount script on function awake:
DontDestroyOnLoad(transform.gameObject);
http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
or you can set the deathCount variable as a playerPref: https://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html so when you die:
PlayerPrefs.SetInt ("deathCount", PlayerPrefs.GetInt("deathCount") + 1);
(untested)
And so, to display the score you would go with a PlayerPrefs.GetInt("deathCount");
I believe that and player prefs calls perform IO operations and should therefore be avoided. Cache the variables ins$$anonymous$$d and only save when the object is destroyed.
The reason why I referred deathCount is because of a game I played (rage game made by EteeskiTutorials and JesseEtzler0 for the Ludum Dare competition) and it had a death counter which was saved, so everytime you came back to play the death count was still the same
$$anonymous$$adJohny - This is the kind of response I was looking for. Thanks.
With the DontDestroyOnLoad command, I assume I need additional code to prevent multiple instances from spawning upon reloading the existing scene? Having the code attached to the player caused rather unfavorable results. I attempted setting up an empty gameobject with the code, but the results were still a little glitchy with the empty gameobject also spawning multiple instances.
So ins$$anonymous$$d of putting the same gameOjbect on the gameplay scene, try putting it in an empty scene, (and not on the gameplay scene), the empty scene shall instaload to the gameplay seen, so you have that one instance, and you don't have more than one, if it works accept my answer pls
Answer by Bennyboy50z · Feb 16, 2014 at 07:33 AM
Hi There! I've also heard a bit that static variables should be avoided but if you do decide to use it for your death counter I just tested your script with a static variable in JavaScript and it worked! Just a few small changes and you should be rolling! Goodluck.
static var DeathCount: int = 0;
var targetGuiText : GUIText;
function OnTriggerEnter(other: Collider) {
if (other.gameObject.tag == "Obstacle1"){
Application.LoadLevel ("Level1");
DeathCount++;
SetDeathText ();
}
}
function SetDeathText(){
targetGuiText.text = "Deaths:" + DeathCount;
}
Thanks for trying it out. I did some tests with static variables and I can manage to make them stay after resetting the scene, but the death counter still isn't staying. I'll keep at it and respond back if I make any progress. Thanks again.
EDIT:
Literally just copied and pasted my own code below the existing code with different variable names and for some reason it works. The death counter doesn't work still, but the new one functions exactly the same.
Seems I can't up vote you yet, but thanks again for the reassurance. I'm still scratching my head at this, but at least it's working now.
Haha that's alright, as you said at least it's working. Glad to have helped.