- Home /
Question by
jamesorion44 · Oct 31, 2020 at 07:58 PM ·
2dscripting problemscore systemadvertising
Save score across scenes
I'm making a mobile game where you are trying to get a high score. I am trying to make a button where when you press it you watch an ad and then you restart with the score that you died with. It's like a second chance. The ad works fine but the score resets to zero. How can I fix this. The function at the bottom is the one attached to the button. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
public class ScoreCounter : MonoBehaviour { public float score;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI highScoreText;
public float highScore;
public TextMeshProUGUI isHighScore;
string gameId = "3885078";
bool testMode = true;
private void Awake()
{
if (PlayerPrefs.HasKey("HighScore"))
{
highScore = PlayerPrefs.GetFloat("HighScore");
highScoreText.text = highScore.ToString();
}
}
void Start()
{
StartCoroutine("Repeater");
Advertisement.Initialize(gameId, testMode);
}
void Update()
{
scoreText.text = score.ToString();
highScoreText.text = highScore.ToString();
HighScore();
PlayerPrefs.SetFloat("Score", score);
}
IEnumerator Repeater()
{
yield return new WaitForSeconds(1.2f);
InvokeRepeating("Counter", .32f, .32f);
}
void Counter()
{
score += 1f;
}
void HighScore()
{
if (score > highScore)
{
highScore = score;
PlayerPrefs.SetFloat("HighScore", highScore);
isHighScore.SetText("New High Score!");
}
if (score < highScore)
{
isHighScore.SetText("Current High Score");
}
}
public void Continue()
{
if (Advertisement.IsReady("rewardedVideo"))
{
Advertisement.Show("rewardedVideo");
SceneManager.LoadScene("Game");
Time.timeScale = 1;
score = PlayerPrefs.GetFloat("Score");
}
}
}
Comment
Best Answer
Answer by Hellium · Oct 31, 2020 at 08:24 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
public class ScoreCounter : MonoBehaviour
{
public float score;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI highScoreText;
public float highScore;
public TextMeshProUGUI isHighScore;
string gameId = "3885078";
bool testMode = true;
private void Awake()
{
if (PlayerPrefs.HasKey("HighScore"))
{
highScore = PlayerPrefs.GetFloat("HighScore");
highScoreText.text = highScore.ToString();
}
// Retrieve the score if saved
if (PlayerPrefs.HasKey("Score"))
{
score = PlayerPrefs.GetFloat("Score");
PlayerPrefs.DeleteKey("Score");
}
}
void Start()
{
StartCoroutine("Repeater");
Advertisement.Initialize(gameId, testMode);
}
void Update()
{
scoreText.text = score.ToString();
highScoreText.text = highScore.ToString();
HighScore();
// DO NOT SAVE SCORE HERE
}
IEnumerator Repeater()
{
yield return new WaitForSeconds(1.2f);
InvokeRepeating("Counter", .32f, .32f);
}
void Counter()
{
score += 1f;
}
void HighScore()
{
if (score > highScore)
{
highScore = score;
PlayerPrefs.SetFloat("HighScore", highScore);
isHighScore.SetText("New High Score!");
}
if (score < highScore)
{
isHighScore.SetText("Current High Score");
}
}
public void Continue()
{
if (Advertisement.IsReady("rewardedVideo"))
{
Advertisement.Show("rewardedVideo");
Time.timeScale = 1;
PlayerPrefs.SetFloat("Score", score);
SceneManager.LoadScene("Game"); // Save score if ad watched
}
}
}