- Home /
Playerprefs to Save UI TEXT
hello everyone. I have two variables in my game. One is for Score and the other is for coin that's defined through UI Text. I want to save number of coins collecting by player. I failed to save and load. here is my script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIManager2 : MonoBehaviour {
public static int coin_score = 0;
public Text coin_text;
void Start () {
}
// Update is called once per frame
void Update () {
coin_text.text = coin_score.ToString ();
}
}
Answer by azerty0220pl · Apr 14, 2020 at 08:57 AM
Something like this should work:
void Start()
{
coin_score = PlayerPrefs.GetInt("anyNameYouLike") //Load value
}
void Update()
{
coin_text.text = coin_score.ToString();
PlayerPrefs.SetInt("anyNameYouLike", coin_score); //Save value
}
You can save a string (PlayerPrefs.SetString("name", stringToSave)), but I do not recommend it in this case. Saving the int is much easier. Also I would recommend updating the score only when it is modified.
Thanks a lot. It worked. but could you please help me in another issue? I have a $$anonymous$$enu in which buttons represent levels and characters. I want to say that if the player has for example 5 coins, That character or level must be unlocked and the piece of wood that acts as a price tag disappear. Every thing worked fine but when i return to my main menu and tried to reload the game, every thing restarted to default. How can I save unlocked stuff?
You can make a variable for each product which will store the status of the product. In the Start() function load the values and depending on their values, unlock the item bought, and save the variable after purchasing a product.
Answer by aminkhosravi007 · Apr 14, 2020 at 10:44 AM
I'm new in unity Your code worked fine but when I try to buy a new item, My coin number doesn't change. I wrote a code in which for any item to buy, number of coins must be decreased. It worked fine before, But now It fails. Should I put your code in Awake ()? @azerty0220pl
You can put my code in Awake(). However I would make it easier by putting it in the Start() function or even making a new function will be probably better, something like this:
void Start()
{
CoinNumberUpdate(); //Calling custom function
}
public void CoinNumberUpdate()
{
coin_score = PlayerPrefs.GetInt("anyNameYouLike");
coin_text.text = coin_score.ToString();
}
And remember to call that function everytime coin_score is changed and saved. For example, if you buy a new product using other script in other gameObject:
public GameObject prevScriptObject; //GameObject to which the previous script is attached to
int prize = 100;
int coin_score;
public void purchase() //Function called by the buy button
{
coin_score = PlayerPrefs.GetInt("anyNameYouLike"); //First load coin_score
if(prize <= coin_score) //Condition
{
coin_score = coin_score - prize; //New coin_score value
PlayerPrefs.SetInt("anyNameYouLike", coin_score); //Save before updating the text on screen
prevScriptObject.GetComponent<PrevScriptName>().CoinNumberUpdate();
}
else
{
//If not suficient money, do something...
}
}
thanks for your time! I appreciate that. I know your code is fine. as my menu is in another scene, I created an empty GameObject and attached my UI$$anonymous$$anager2. renamed it to for example "Reference" and started writing my code. I wrote a code in which for every 40 points, my coins must be increased for 1 unit. For your first code, I think coin_text.text = coin_score.ToString(); must be in Update() because it didn't work. here is my second script in which when player clicks on the button (WhitePlane) something happens
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
using UnityEngine.UI;
public class $$anonymous$$ain$$anonymous$$enu3 : $$anonymous$$onoBehaviour {
public Button WhitePlane;
public GameObject Reference;
public static int character_number;
WhitePlane.onClick.AddListener (() => {
character_number=2;
if (UI$$anonymous$$anager2.coin_score>=1){
UI$$anonymous$$anager2.coin_score--;
Scene$$anonymous$$anager.LoadScene ("$$anonymous$$enu2");
}
});
I tried your codes but I thinks I must have missed one step. Could you please write your second code into my second script? @azerty0220pl
Your answer
Follow this Question
Related Questions
How do I save and load the value of a text? 1 Answer
Unity Won't Load 0 Answers
Coding help: How to use xml serialization 1 Answer