Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SonicDirewolf · Nov 18, 2017 at 03:22 PM · sceneplayerprefsscene-loadingscorerestart

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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) ;
      }
 }


Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SonicDirewolf · Nov 18, 2017 at 05:58 PM 0
Share

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!!!

avatar image ruimcp95 · Sep 15, 2019 at 05:36 PM 0
Share

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.

avatar image Hellium ruimcp95 · Sep 15, 2019 at 05:44 PM 1
Share

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.

avatar image ruimcp95 Hellium · Sep 15, 2019 at 07:44 PM 0
Share

You were right. It was a typo. eheheh Thank you!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges