I am having trouble of keeping my death count after restarting the level this is the code I am using.
public int deaths;
void Start(){
deaths = PlayerPrefs.GetInt ("Deaths");
}
void OnCollisionEnter(Collision hit){
if(hit.gameObject.tag == "Enemy"){
PlayerPrefs.SetInt("Deaths", deaths);
deathCount();
Application.LoadLevel("FirstGameLvl 1");
}
}
void OnGUI () {
GUI.Box (new Rect (200, 40, 100, 20), "Deaths: " + deaths);
}
public void deathCount () {
if (deaths < 0)
deaths = 0;
deaths += 1;
}
It counts the death for a split second but loading the level again just restarts my death. Can someone help me with this please?
This is the solution I came up with.
public int deaths;
private bool dead;
void Start(){
deaths = PlayerPrefs.GetInt ("Deaths");
dead = false;
}
void Update(){
if (dead) {
transform.position = new Vector3(0f,0.6f,-2f);
transform.rotation = Quaternion.identity;
deaths++;
dead = false;
}
}
void OnCollisionEnter(Collision hit){
if(hit.gameObject.tag == "Enemy"){
dead = true;
PlayerPrefs.SetInt("Deaths", deaths);
PlayerPrefs.GetInt("Deaths");
}
}
void OnGUI () {
GUI.Box (new Rect (200, 40, 100, 20), "Deaths: " + PlayerPrefs.GetInt("Deaths"));
}
But it doesn’t count the first death but after that first death it works perfectly would you happen to know the reason behind why it doesn’t count the first death?
public int deaths;
void OnCollisionEnter(Collision hit){
if(hit.gameObject.tag == "Enemy"){
PlayerPrefs.SetInt("Deaths", deaths);
deaths++;//Adds one to the count
Application.LoadLevel("FirstGameLvl 1");
}
}
void OnGUI () {
GUI.Box (new Rect (200, 40, 100, 20), "Deaths: " + PlayerPrefs.GetInt ("Deaths"));
}
So what i changed is:
1.I deleted the function deathCount because its unneeded(atleast from what i understood you are trying to do)
2.I changed this line GUI.Box (new Rect (200, 40, 100, 20), "Deaths: " + PlayerPrefs.GetInt (“Deaths”));
Should work now