0

Hello everybody i'm having an issue with the score and the currency thing, the game had a coins so the player can collect them so what i want to, the coins add to the currency text ,every time when i play automatically they increase what i took coins in the game and at the end to load and save the currency so when i re-open the game so i can see the collected coins doesn't changed

Here's the GameManager script

public Text scoreText, coinText, modifierText, hiscoreText;
private float score, coinScore, modifierScore;

    public void GetCoin()
{
    diamondAnim.SetTrigger("Collect");
    coinScore++;
    coinText.text = coinScore.ToString("0");
    score += COIN_SCORE_AMOUNT;
    scoreText.text = score.ToString("0");
}

Here's the shopPanel script

//test
public static int money = 0;
public Text coinsTxt;
public Text currencyTxt;
public static int totalMoney;

void Awake()
*{
         currencyTxt = PlayerPrefs.GetInt ("Coins");
}*
private void Update()
{
     coinsTxt.text = "Coins: " + currencyTxt;
    coinsTxt.text = "" + money;
}

void TotalMoney()
{
    money += totalMoney;
    currencyTxt.text = "" + totalMoney;
}

Coin script !

public class Coin : MonoBehaviour {

private Animator anim;

private void Awake()
{
    anim = GetComponent<Animator>();
}


private void OnEnable()
{
    anim.SetTrigger("Spawn");
}

private void OnTriggerEnter(Collider other)
{
    if (other.tag == "Player")
    {

        //CurrencyManager.gameManager.AddMoney(1);
        GameManager.Instance.GetCoin();
        anim.SetTrigger("Collected");
    }
}

}

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • There is no save code in your question. You didn't even bother to Google "how to save variable in Unity". There are many posts about PlayerPrefs out there – Programmer Oct 21 '17 at 17:06
  • I read about playetPrefs and i tried some and didn't work so i erase them from the code ;) – x'D Nasoor Oct 21 '17 at 17:49
  • No problem. Next time your code is not working, post the non working code and describe what you mean by "doesn't work". It will show people that you have tried something and they will use that code to help you. No need to erase it. – Programmer Oct 21 '17 at 18:45
  • Okay so should i edit the code that i erased them ? – x'D Nasoor Oct 21 '17 at 19:19
  • You don't have to. This question is closed. See the duplicate link for a better way to save data in Unity – Programmer Oct 21 '17 at 23:15
  • I edited it , its not closed tho – x'D Nasoor Oct 22 '17 at 13:53
  • And i read it yesterday , I'm not using Json i'm using C# – x'D Nasoor Oct 22 '17 at 13:54
  • It is closed which means no one can answer it unless it is re-opened. Your second comment is funny. I think you should learn the difference between Json and C#. One is just a data format to save your file in and the other one is a programming language – Programmer Oct 22 '17 at 15:43
  • I read about Json before one hour , i have a question currency doesn't work what should i do ? – x'D Nasoor Oct 22 '17 at 15:50
  • Hey, read the duplicate then try to do what's there. Edit your question with your new code and tell me which part is not working – Programmer Oct 22 '17 at 15:59
  • It's in the code the shopPanel script which is currencyTxt – x'D Nasoor Oct 22 '17 at 16:00
  • What i want is when player collect the coins , they appear in the currencyText which is in the shopPanel and every time when he play and collect coins the currency increase – x'D Nasoor Oct 22 '17 at 16:02
  • I know it's been closed and quite old, but you're reading a PlayerPref you've never set in the first place. In GetCoin, you should set your playerpref after incrementing coinScore, then when you come to the shop panel and reading it, it'll be there. – laminatefish Oct 27 '17 at 14:03

0 Answers0