How to Clear Score Play Again Unity
- Spaces
- Default
- Assist Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Home /
0
Question by felsy · Dec 18, 2014 at 05:34 AM ·
Reset score to zero on game restart
Hello, i want to reset my score back to zero when game restarts. my score scripts loads score from ane scene to some other, that is fine for me just when it game over and information technology restart the one-time score still shows. i want it to restart back to zero.
using UnityEngine; using UnityEngine.UI; using Arrangement.Collections;
public class ScoreManager : MonoBehaviour { public static int score;
Text text; void Awake () { text = GetComponent <Text> (); score = 0; score = PlayerPrefs.GetInt ("Score"); } void Update () { text.text = "Score: " + score; PlayerPrefs.SetInt ("Score", score); }
}
2 Replies
· Add your respond
- Sort:
1
Answer past fafase · Dec xviii, 2014 at 06:27 AM
Your problem is that you are constantly setting the value in PlayerPrefs, you should simply do it when necessary, that is when you update the score or more probable when you finish the level.
void EndOfLevel(bool success){ if(success){ PlayerPrefs.SetInt("Score", score); } else{ score = 0; } }
You lot phone call that method either on win or on game over.
create a method
private void resetScore(){ PlayerPrefs.SetInt("Score", 0); }
and call information technology wheneve you want to reset score
i added the resetScore() only it didn't work. here is the script.
using UnityEngine;
public class GameOver$$anonymous$$anager : $$anonymous$$onoBehaviour { public PlayerHealth playerHealth; public float restartDelay = 5f;
Animator anim; bladder restartTimer; void Awake() { anim = GetComponent<Animator>(); } void Update() { if (playerHealth.currentHealth <= 0) { anim.SetTrigger("GameOver"); restartTimer += Time.deltaTime; if (restartTimer >= restartDelay) { Application.LoadLevel(Awarding.loadedLevel); } } } private void resetScore() { PlayerPrefs.SetInt ("Score", 0); }
}
Ok, thats a step closer, now you need to call the function of resetScore. At present this is probably best to do right under the application.loadLevel. The two scripts should wait like this:
Death:
using UnityEngine; public grade GameOver$$anonymous$$anager : $$bearding$$onoBehaviour { public PlayerHealth playerHealth; public float restartDelay = 5f; Animator anim; bladder restartTimer; void Awake() { anim = GetComponent<Animator>(); } void Update() { if (playerHealth.currentHealth <= 0) { anim.SetTrigger("GameOver"); restartTimer += Time.deltaTime; if (restartTimer >= restartDelay) { Application.LoadLevel(Awarding.loadedLevel); resetScore(); } } } void resetScore() { PlayerPrefs.SetInt ("Score", 0); } }
Other script: using UnityEngine; using UnityEngine.UI; using System.Collections;
public class Score$$bearding$$anager : $$bearding$$onoBehaviour { public static int score; Text text; void Awake () { text = GetComponent <Text> (); score = PlayerPrefs.GetInt ("Score"); } void Update () { text.text = "Score: " + score; PlayerPrefs.SetInt ("Score", score); }
O I removed all the comments that were obsolete and making it incommunicable to read. First how do you set the game over or game win? This is where you should brand it happen. Let's say you either achieve a trigger box (Win) or you run out of health (Lose).
individual int wellness; public void Wellness{ become{return health;} set{ health-=value; if(health <= 0){ EndOfLevel(false); } } } void OnTriggerEnter(Collider col) { EndOfLevel(truthful); } void EndOfLevel(bool success){ if(success){ PlayerPrefs.SetInt("Score", score); } else{ score = 0; } }
@ fafase. my game doesn't take a game win notwithstanding, but it has a game over when the player runs out of wellness. but my game is based on a timer. if the player survivals in threescore seconds in 1st scene then the adjacent scene loads and that should load the score form the 1st scene to the adjacent scene and when player dies in any scene the score should reset back to 0.
i
Answer by Kiwasi · Dec 18, 2014 at 08:09 AM
void Awake () { if(Awarding.loadedLevelName == "The Kickoff Level In My Game"){ PlayerPrefs.SetInt ("Score", 0); } // Do the other stuff you already do }
it doesn't reset the score.
Yes it does. You lot just lack the skill to implement it with your existing lawmaking base.
Stride through your logic line by line until yous detect the mistake.
should i add together this to the score script or the game over script?
As written it goes in Score$$bearding$$anager, every bit part of the existing Awake method.
Finally it worked. but it works simply in the beginning scene. it loads the score to the next scene but when it game over in the next scene it doesn't reset the score back to zero. i am thinking its application.loadedlevelName
Show more comments
Your answer
Welcome to Unity Answers
The best identify to ask and answer questions near development with Unity.
To help users navigate the site we have posted a site navigation guide.
If y'all are a new user to Unity Answers, check out our FAQ for more information.
Brand sure to check out our Knowledge Base for commonly asked Unity questions.
If y'all are a moderator, see our Moderator Guidelines page.
Nosotros are making improvements to UA, see the listing of changes.
Follow this Question
Source: https://answers.unity.com/questions/857357/reset-score-to-zero-on-game-restart.html
Non going to post this as an answer, as Im not sure if it is correct but, when you say score = 0; have it as PlayerPrefs.SetInt("Score", 0);
that worked when the game restarts, score also restart to 0. But the score cannot load to adjacent scene. If i score 50 in scene 1 information technology should load 50 in scene 2 to continue scoring but merely it loads naught.
Y'all really have to ascertain the weather for setting the score dorsum to zero. One time you accept a logical description of when you desire to reset the score and then you tin can utilize either of the answers below.