- Home /
[SOLVED] Same script (virtually) on two gameObjects. One saves to playerprefs, one doesn't
Ugh. Sorry I'm asking this. I have two gameobjects, boss characters. Each has (nearly) the same script attached, a health script. The one works perfectly, and saves the result of the battle to playerprefs, so that after you've defeated him, he's gone for good.
But the other one...
The results of the battle never save. He just reappears every time the game is started, and I don't get it. Clearly, I've done something wrong, can't see something, have bad syntax somewhere, I don't know. Am hoping fresh eyes might see what I'm missing, because I'm banging my head against the keyboard after spending hours on this. The two scripts are below, starting with the working one. God bless, and thanks. Sorry to ask such a stupid question.
Working Script
var Name = "Guardian";
var Exp = 150;
static var GuardianHealth : int = 75;
var GuardianKilled = 0;
function Start(){
if(PlayerPrefs.GetInt("GuardianDead", 0) == 1){
//object.active = true;
//object.enabled = true;
TowerGateOpen.GateOpen = true;
Destroy(gameObject);
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.name == "sword") {
GuardianHealth = -10;
}
else
{
if (other.gameObject.name == "club")
{
GuardianHealth = -1;
}
if(GuardianHealth <= 0 )
if(GuardianKilled == 0 )
{
GuardianHealth = 0;
GuardianKilled = 1;
audio.Play();
Playerhealth.curXp += Exp;
Playermoney.curMoney += 350;
TowerGateOpen.GateOpen = true;
PlayerPrefs.SetInt("GuardianDead", 1);
Debug.Log( Name + " is killed");
Destroy(gameObject);
}
}
}
Script that won't save to playerprefs:
var Name = "Medamomo";
var Exp = 450;
static var MedamomoHealth : int = 5;
var MedamomoKilled = 0;
function Start(){
if(PlayerPrefs.GetInt("MedadmomoKilled", 0) == 1){
//object.active = true;
//object.enabled = true;
MedamomoGateOpen.GateOpen = true;
Destroy(gameObject);
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.name == "sword") {
MedamomoHealth = -7;
}
else
{
if (other.gameObject.name == "club")
{
MedamomoHealth = -1;
}
if(MedamomoHealth <= 0 )
if(MedamomoKilled == 0 )
{
MedamomoHealth = 0;
MedamomoKilled = 1;
//audio.Play();
Playerhealth.curXp += Exp;
Playermoney.curMoney += 1550;
PlayerPrefs.SetInt("MedamomoKilled", 1);
Debug.Log( Name + " is killed");
MedamomoGateOpen.GateOpen = true;
Destroy(gameObject);
}
}
}
Can you see if you can get the indenting working properly? I'm struggling to make much sense of it with the current formatting!
Is there a reason the Health variables are static and the $$anonymous$$illed ones aren't?
For sword strikes do you mean to do health -= hitAmount? Currenty sword strikes and club strikes are instant kills as you set the health to 0 or -something.
Have you checked that the $$anonymous$$edamomo$$anonymous$$illed variable doesn't have a different value set in the inspector? This could cause it to be something different to 0 to start with, meaning that the killed logic (inc. setting PlayerPrefs) won't be execited
hey, still working on my formatting, but it was a really simple fix. I misspelled something, which I kinda suspected.
As of right now, both sword and club ARE one hit kills. That will change for the player, just set his hp low for testing purposes.
Answer by Josh707 · Nov 07, 2013 at 04:01 AM
Between lines 5 & 10, you seem to have misspelled "MedamomoKilled" as "MedadmomoKilled", since player prefs don't throw errors with unset values that might be the issue.
that was it man. You are totally awesome, thank you!!!!!
Answer by Owen-Reynolds · Nov 07, 2013 at 04:08 AM
Just glancing, GuardianHealth is a static, which is reset to 0(?). This means when the 1st guy is killed, the 2nd guy has 0 health. Static is really special purpose. A "normal" variable would give them each their own health. Then sword and club hits have GuardianHealth = -10;
, meaning they insta-kill the guy. You probably meant to use -=
to subtract 10 health.
Then the death check (the two ifs) specifically says to ignore death if you're already killed a guy.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Playerprefs not work!? 0 Answers
Health bar depletes when app isn't open 1 Answer
update highscore if current is higher than previous score 0 Answers
this is part of my health script it gives me a error unknown identifier GameScore pls help thxxx 0 Answers
Health script, save health in between scenes. PlayerPrefs. 2 Answers