Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Save score after a Level, then use this to unlock things in the Menu. Unity 2D

Posted on November 14, 2022

Solution 1 :

I would use Unitys PlayerPrefs System.

Example:

Setting Score:

public Text score;
int currentscore = 0;

Increasing Score:

currentscore += 1;
score.text = currentscore.ToString();

Setting HighScore and Loading Menu:

GameManager.SetHighScore(currentscore);
SceneManager.LoadScene("Menu");

Functions to Load/Change PlayerPrefs:

void Start()
{
    highscore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
}

public static void SetHighScore(int score)
{
   if (score > PlayerPrefs.GetInt("HighScore", 0))
   {
       PlayerPrefs.SetInt("HighScore", score);
   }
}

Your Code adapted to PlayerPrefs:

public int currentSpears = 0;

public GameObject rune1;
public GameObject rune2;
public GameObject rune3;

void Start()
{
    currentSpears = PlayerPrefs.GetInt("CurrentSpears", 0).ToString();
}

public static void IncreaseSpears()
{
    int spears = PlayerPrefs.GetInt("CurrentSpears", 0);
    PlayerPrefs.SetInt("CurrentSpears", spears++);
}

public void RuneCollection()
{
    if (currentSpears >= 2)
    {
        Debug.Log("3 runes collected!");
        rune3.SetActive(true);
        totalRunes = 3;
    }
    else if (currentSpears >= 1)
    {
        Debug.Log("2 runes collected!");
        rune2.SetActive(true);
        totalRunes = 2;
    }
    else
    {
        Debug.Log("1 rune collected!");
        rune1.SetActive(true);
        totalRunes = 1;
    }
}

Solution 2 :

You can save your data with PlayerPrefs for some single values or PersistentDataPath for some multiple values such as a class. Find out more with this link, and you can use them as below.

PlayerPrefs:

private int score = 0;

//set value
PlayerPrefs.SetInt("Score", score);

//get value
private int savedScore = PlayerPrefs.GetInt("Score");

PersistentDataPath:

private string savedName;
private int savedHealth;
private string loadedName;
private int loadedHealth;

public void Save(){
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath + "/FileName.dat", 
         FileMode.Create);
    PlayerClass newData = new PlayerClass();
    newData.health = savedHealth;
    newData.name = savedName;
    bf.Serialize(file, newData);
    file.Close();
}

public void Load(){

    if (File.Exists(Application.persistentDataPath + "/FileName.dat")){
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Open(Application.persistentDataPath + "/FileName.dat", FileMode.Open);
        ObjData newData = (ObjData)bf.Deserialize(file);
        file.Close();
        loadedHealth = newData.health;
        loadedName = newData.name;
    }
}

[Serializable]
class PlayerClass{
    public string name;
    public int health;

}

Problem :

Im currently working on a little mobile game in which you throw spears at targets, and depending on how many spears you have left at the end of the level you get between one and three Runes (similar to the Star scoring sytem in Angry Birds). The plan is to save the runes and use them to unlock different spear variants in the Main Menu.
At the moment you can see how many runes you got after completeing the level, but the values are not yet being saved.
My question is, how would I go about saving the Runes after each Level? Any ideas? You can see the part of the script Im using for that below. Thanks in advance.

public int totalRunes = 0;

public GameObject rune1;
public GameObject rune2;
public GameObject rune3;
public void RuneCollection()
{
    if (currentSpears >= 2)
    {
        Debug.Log("3 runes collected!");
        rune3.SetActive(true);
        totalRunes = 3;
    }
    else if (currentSpears >= 1)
    {
        Debug.Log("2 runes collected!");
        rune2.SetActive(true);
        totalRunes = 2;
    }
    else
    {
        Debug.Log("1 rune collected!");
        rune1.SetActive(true);
        totalRunes = 1;
    }
}
READ  [ANSWERED] android - Espresso click two items in recycler view
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress