Reputation: 147
I have a score in my game that increments at certain points! this is working fine but i want a saved high score and unsure how to implement playerprefs as i am still getting to grips with unity and c#! so far i have placed a ui text element in my canvas and this is called High Score: 0 i would like the 0 to hold and save the high score! the working score that increments at the moment is in the update method. EDIT, just to point out this is not a duplicate of a link posted below! both are looking for different answers to a question that seems similar but is not
public class Player : MonoBehaviour {
public string currentColor;
public float jumpForce = 10f;
public Rigidbody2D circle;
public SpriteRenderer sr;
public Color blue;
public Color yellow;
public Color pink;
public Color purple;
public static int score = 0;
public Text scoreText;
public GameObject obsticle;
public GameObject colorChanger;
void Start () {
setRandomColor ();
circle.isKinematic = true;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Jump") || Input.GetMouseButtonDown (0))
{
circle.isKinematic = false;
circle.velocity = Vector2.up * jumpForce;
}
scoreText.text = score.ToString ();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Scored")
{
score++;
Destroy (collision.gameObject);
Instantiate (obsticle, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag == "ColorChanger")
{
setRandomColor ();
Destroy (collision.gameObject);
Instantiate(colorChanger, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag != currentColor) {
Debug.Log ("You Died");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
score = 0;
}
if (collision.tag == "Floor")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
void setRandomColor()
{
int rand = Random.Range (0, 4);
switch (rand)
{
case 0:
currentColor = "Blue";
sr.color = blue;
break;
case 1:
currentColor = "Yellow";
sr.color = yellow;
break;
case 2:
currentColor = "Pink";
sr.color = pink;
break;
case 3:
currentColor = "Purple";
sr.color = purple;
break;
}
}
}
Upvotes: 1
Views: 132
Reputation: 180
There doesn't appear to be anything wrong with incrementing the score. If you want to save a high score and have it persist across games, I recommend making a .txt file in your Assets folder, and writing the score to it if it's greater than what's in the file. I haven't tested it because I'm on my phone but something like this should work:
using System.IO;
public Text highScoreText;
void Start() {
highScoreText.text = File.ReadAllText(TEXTFILEPATH);
}
if (collision.tag != currentColor) {
Debug.Log ("You Died");
if (File.Exists(TEXTFILEPATH) {
int highScore = int.TryParse(File.ReadAllText(TEXTFILEPATH);
if(score > highScore) {
File.WriteAllText(TEXTFILEPATH, score.ToString());
}
else {
File.WriteAllText(TEXTFILEPATH, score.ToString());
}
}
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
score = 0;
}
Upvotes: 3