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();
}
// 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.
↧