i have a multi scenes play and each scenes i have “coinmanager” script (like below). so i want the coin collect each scenes go to main menu scene or shop scene ( every “scenes play” adding coin get in menu scene or shop scene)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Coinmanager : MonoBehaviour {
public Text coinText;
public float coinAdd = 0f;
public float coinCount;
public float TopCount;
void Start ()
{
}
void Update () {
{
coinAdd = coinCount ;
}
PlayerPrefs.SetFloat (“Coin”, CoinCount);
}
coinText.text = " " + Mathf.Round (coinCount);
}
public void AddCoin (int CoinToAdd)
{
coinCount += CoinToAdd;
}
}
how to declare the :
void start () {
if (PlayerPrefs.HasKey(“Coin”))
{
CoinCount = PlayerPrefs.GetFloat (“Coin”);
}
in the new scene and how to collect coin we have in each scenes??
i mean : scene 1 + scene 2 + scene 3 + scene 4 = collcetable scene ( in main menu scene).
I think you have some extra braces in your Update() method. Your start method must be spelled “Start” with a capital ‘S’… as for your question/thoughts - that’s all there is to it, if you’re saving with player prefs. Save and load.
Though, I would suggest that you delete your Update method as it appears to be doing nothing useful. Simply move the player prefs saving and text setting to the method where you actually add/gain a coin.
Can you be more specific if something is not working and what’s not working, if so?
Update metode in this scrit is connected with another script.
my problem what i mean is how to declare the scipt coinmanager that i have;
scene 1 (coinmanager1) + scene 2 (coinmanager2)+ scene 3 (coinmanager3 )+ scene 4 (coinmanager4) = total coin that i have collectable in each scenes play save in main menu scene.
yea… i want to declare the player pref the coinmanager scipt in mainmenu scene, but i dont know to declare that.
than how can i declare the sum scene 1 (coinmanager1) + scene 2 (coinmanager2)+ scene 3 (coinmanager3 )+ scene 4 (coinmanager4), where the total sum in a place main menu?
I just don’t understand at which part you’re lost. You kinda just keep repeating your plan to me.
So, just load the Coin(s) from player prefs when you’re in the main menu, and it will show you the value … including any previously saved/collected coins that you added / saved to PlayerPrefs on any other level.
yea… but i dont know how can i call the each script “coin manager” in scenes play to the scene main menu script “total collect coin” with playerprefs?
i dont have idea about that…
You don’t. You’re saving to a playerpref. So, you go to scene1, get the value of your playerpref and store the value in a variable. (value 0 if you have no coins). So you collect 10 coins. You save this back out to your playerpref.
You go to scene 2 and get the value of your playerpref. You now have 10 coins at the start of scene 2. You collect another 10 coins, you now have 20 coins. Then you save this back to the playerpref.
You go to the main menu, you get the value of your playerpref. It shows you have 20 coins.
You just have to load the value at the start of each scene, add or subtract from that value, then at the end of the scene, save the value out (or more often if they are spending/earning during the scene if you need to, depending on your use case)
Unfortunately, since you’re not sure what I’m saying and I can’t understand if what you want is what I think you want or not, I don’t think I can help more atm.
Hopefully someone else can maybe help …
if (PlayerPrefs.HasKey("Coin"))
{
CoinCount = PlayerPrefs.GetFloat ("Coin");
}
Though, you don’t need the ‘has key’, since if it’s not there, the default value it will give you is zero…anyhow, also doesn’t really hurt anything, either.
@methos5k Showed you how to get your coins from PlayerPrefs, which is actually just from your code. You just need to get the coins at the start of a scene. Then display that value on some text. Subtract or add from that value and update your text and save back to your playerpref.
I have no idea what you want done when you say fix a script to work in another scene. You can add a script to any scene you want. You don’t have to even use the same script depending on what you need done, so you have to do that yourself and figure out what you want.
And honestly, I personally wouldn’t do anything with this part of your code in Update. It’s not needed. Set the stuff up once and only update text when your values change.