how to declare coin get in many scenes play to the a main menu or scene shop?

hy maaster, i need some help. please help me.

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).

thanks

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.

how to declare that bro methos5k.

Alright – do you want the coins to be distinguished by which scene they were gathered in?

Otherwise, if it’s merely the sum… as I was saying you already have the idea by saving and loading the value to/from PlayerPrefs.

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. :slight_smile:

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 …

Right… hopefully the post above helps! :slight_smile:

1 Like

that is what i mean…but would you explain about load method… because i don’t understand about that…please help

thanks you help

For PlayerPrefs, it’s just this:

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. :slight_smile:

And I copied that from your own post …

can you fix this script for can call in another scene ( menuscene) because i dont have any idea;

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;
}

}

can you help me ??
i dont know how to use many script in diffrent scene collect in a scene menu like my case

@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.

script 1;
public Text coinText;
public float coinAdd = 0f;
public float coinCount;
public float TopCount;

void Start ()
{
if (PlayerPrefs.HasKey(“Coin”))
{
CoinCount = PlayerPrefs.GetFloat (“Coin”,CoinCount);
}

}
void Update () {
coinAdd = coinCount ;
PlayerPrefs.SetFloat (“Coin”, CoinCount);
}
coinText.text = " " + Mathf.Round (coinCount);
}
public void AddCoin (int CoinToAdd)
{
coinCount += CoinToAdd;
}
}

script 2;

public float coinCount;

void Start ()
{

CoinCount = PlayerPrefs.SetFloat (“Coin”,CoinCount);
}

}
void Update () {

PlayerPrefs.SetFloat (“Coin”, CoinCount);
}
coinText.text = " " + Mathf.Round (coinCount);
}

is this right, Brathnann?

Update your code with code tags Using code tags properly

Way to hard to read this.

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.

my update metode here, connect to another script that i have. i think that not needed in this case.

void Update () {
coinAdd = coinCount ;

}
coinText.text = " " + Mathf.Round (coinCount);
}
public void AddCoin (int CoinToAdd)
{
coinCount += CoinToAdd;
}

that is the script what i mean

public int scorecoin = 1;
private ScoreManager theScoremanager;
public AudioClip coinCollectSound;
// Use this for initialization
void Start () {

theScoremanager = FindObjectOfType ();
}

// Update is called once per frame
void Update () {

}
void OnTriggerEnter2D (Collider2D coin)
{
if(coin.gameObject.tag == “player”)
{
theScoremanager.AddCoin(scorecoin);
gameObject.SetActive (false);
}
}

What’s trying to be said here, is your method…

public void AddCoin (int CoinToAdd)
{
   coinCount += CoinToAdd;
   coinText.text = " " + Mathf.Round (coinCount); // though, the coinCount could be int
}

and just erase your Update method, as it’s not doing anything that isn’t done now in the method.
I don’t know what ‘coinAdd’ does at all.

If you haven’t already done so, your script #2 should just be another instance of script #1, I believe.