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 ThePuppetMaster · Jun 13, 2016 at 06:09 AM · playerprefsscore systemstore

Change store balanced based on level scores.

I have been trying to have the balance for an in game store based on scores from levels. I have code that gets the highscore from all the level and puts it in an array that is in a script on a gameobject that is created on the first scene and has a dontdestroyonload in the script, so the highscore can be displayed on the level select screen underneath all of the levels. Each score in the array is equal to a playerprefs that is set to at the end of each level only if the player has improved their previous score. I have an in game shop that I want the balance of the player to equal the sum of all the ints the array, which can be done by having .Sum() at the end of the end of the array, such as if the int array is named numbers, it would be numbers.Sum(). The issue is two things, 1). The array will be set every time the game is played, and therefore if any money is deducted previously, it will reappear as all of the ints in the array combined. I can solve this by having it set to a playerprefs and only have it set if the actual array is equal to, and this would only ever set this once, but that causes the player to never be able add to the balance if they receive a higher score. Here are some of the scripts and I know this might be hard to understand, but please comment if you need more information.

 using UnityEngine;
 using System.Collections;

 public class ScoreKeeper : MonoBehaviour {

 public int[] scores;
 public string[] playerPrefStrings;
 public LevelManager lvlManage;
 public int coinsStore;

 // Use this for initialization
 void Start () {
     
 }

 // Update is called once per frame
 void Update () {
     scores[0] = PlayerPrefs.GetInt(playerPrefStrings[0]);
     scores[1] = PlayerPrefs.GetInt(playerPrefStrings[1]);
     scores[2] = PlayerPrefs.GetInt(playerPrefStrings[2]);
     scores[3] = PlayerPrefs.GetInt(playerPrefStrings[3]);
     scores[4] = PlayerPrefs.GetInt(playerPrefStrings[4]);
     scores[5] = PlayerPrefs.GetInt(playerPrefStrings[5]);
     scores[6] = PlayerPrefs.GetInt(playerPrefStrings[6]);
     scores[7] = PlayerPrefs.GetInt(playerPrefStrings[7]);
     scores[8] = PlayerPrefs.GetInt(playerPrefStrings[8]);
     scores[9] = PlayerPrefs.GetInt(playerPrefStrings[9]);
 }

 void Awake()
 {
     DontDestroyOnLoad(gameObject);
 }
 }

This is the script that is attached to the game object that always exist and has the array for the high scores.

 using System;
 using UnityEngine;
 using GoogleMobileAds.Api;
 
 public class LevelManager : MonoBehaviour {
     
     public Font myFont;
     public int score;
     public int life;
     public float temp;
     public float temp2;
     public float temp3;
     public float temp4;
     public int fontsize2;
     public int fontsize3;
     public int lvlNum;
     public int nextLvl;
     public string playerPrefsString;
     public StarFinish starFinish;
     public AIMovement enemyMove;
     public GUIText scoreTxt;
     public GUIText highscoreTxt;
     public ScoreKeeper scoreKeep;
     public GameObject pressScreen;
     public GameObject newhighscore;
     public GameObject scoreKeepObj;
     private int fontsize;
     public int currentLvl;
     private bool hasPlayed;
     private InterstitialAd interstitial;
     
     // Use this for initialization
     void Start () {
         hasPlayed = false;
         RequestInterstitial ();
         currentLvl = Application.loadedLevel - 3;
         scoreKeepObj = GameObject.FindWithTag("ScoreKeeper");
         scoreKeep = scoreKeepObj.GetComponent<ScoreKeeper>();
 
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             Application.LoadLevel(lvlNum);
         }
         if (enemyMove.ifDead == true) {
             print("You Died!");
 //            Application.LoadLevel(nextLvl);
         }
         if (starFinish.ifFinished) {
             if(score>PlayerPrefs.GetInt(playerPrefsString)) {
                 PlayerPrefs.SetInt(playerPrefsString,score);
 //                scoreKeep.scores[currentLvl] = PlayerPrefs.GetInt(Application.loadedLevel + "Best");
 //                newhighscore.SetActive(true);
             }
 
 //          scoreKeep.scores[currentLvl] = PlayerPrefs.GetInt(Application.loadedLevel + "Best");
 
             ShowInterstitial();
             //pressScreen.SetActive(true);
             Application.LoadLevel(nextLvl);
 
             if(!hasPlayed){
                 hasPlayed = true;
             }
         }
     }

This is the script that each level has that gives the previous script the high scores.

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
0
Best Answer

Answer by jgodfrey · Jun 14, 2016 at 12:03 AM

I could me missing something, but why not...

  • Keep track of the money spent in a separate player prefs record

  • Sum the level scores in your array just as you suggest

  • Subtract the amount of money spent

  • The result should be the player's balance

You'd just need to keep track of the money spent...

Comment
Add comment · Show 1 · 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 ThePuppetMaster · Jun 14, 2016 at 12:29 AM 0
Share

Thanks, sorry the question was kind of worded weirdly, wasn't sure exactly how to state my problem, but this is exactly what I needed, thank you so much!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to add playerPrefs to this script? 1 Answer

Buy skins and save which ones I have bought in one playerprefabs 1 Answer

create a store to purchase gameobjects for 2d game like in color switch 0 Answers

One PlayerPref not saving 1 Answer

Player Prefs not saving 0 Answers


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