- Home /
Restarting game with last score from playerprefs. PLEASE HELP!!
I want to start my game with the last score that the player achieved whenever he watches a rewarded ad. I am using admob. This is the ad script.
public class AdManager : MonoBehaviour {
public static AdManager instance;
public RewardBasedVideoAd rewardBasedVideo;
void Start()
{
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
#if UNITY_ANDROID
string appId = "ca-app-pub-3940256099942544~3347511713";
#else
string appId = "unexpected_platform";
#endif
MobileAds.Initialize(appId);
this.RequestRewardedVideo();
}
public void RequestRewardedVideo()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#else
string adUnitId = "unexpected_platform";
#endif
AdRequest request = new AdRequest.Builder().Build();
this.rewardBasedVideo.LoadAd(request, adUnitId);
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
SceneManager.LoadScene ("Game"); // here I want to load this scene as I do in simple restart but with the last achieved score and not just from 0.
}
}
This is my score manager script
using UnityEngine;
using System.Collections;
public class ScoreManager : MonoBehaviour {
public static ScoreManager instance;
public int score;
void Awake(){
if (instance == null) {
instance = this;
}
}
void Start () {
score = 0;
PlayerPrefs.SetInt ("Score", 0);
}
void Update () {
}
public void IncrementScore(){
score++;
}
public void StopScore(){
PlayerPrefs.SetInt ("Score", score);
if (PlayerPrefs.HasKey ("HighScore")) {
if (score > PlayerPrefs.GetInt ("HighScore")) {
PlayerPrefs.SetInt ("HighScore", score);
}
} else {
PlayerPrefs.SetInt ("HighScore", score);
}
}
}
Your help is so much appreciated! Thank You!
Answer by Hellium · Nov 18, 2017 at 04:40 PM
Try this :
public class AdManager : MonoBehaviour
{
// ...
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
PlayerPrefs.SetInt ("ScoreReward", 1);
SceneManager.LoadScene ("Game");
}
}
public class ScoreManager : MonoBehaviour
{
void Start ()
{
bool scoreReward = PlayerPrefs.GetInt ("ScoreReward", 0) > 0;
score = scoreReward ? PlayerPrefs.GetInt ("Score", 0) : 0;
PlayerPrefs.SetInt( "Score", 0) ;
PlayerPrefs.SetInt("ScoreReward", 0) ;
}
}
Thank You so much for this! It was very clever the way you used the bool. I was thinking of creating a whole new scene xd. Thank You again!!!
Hello. I'm trying to achieve the exact same result as @SonicDirewolf , but I can't seem to make the code work. Is this all of it? Thank you.
Are you sure the keys you provide to PlayerPrefs
are correct? You may have done a typo. The keys must be perfectly identical, the case is important.
You were right. It was a typo. eheheh Thank you!
Your answer
Follow this Question
Related Questions
How to Add and Keep Score with PlayerPrefs for the Next Scene? 1 Answer
How to load (restart) last scene 2 Answers
1 Scene game|| Reset variables 1 Answer
I'm trying to set a high score but I can't display it in another scene? 2 Answers
Disappear objects and that these are still missing when you return to load the scene 1 Answer