Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

how to save cooins after reopen the app?

Discussion in 'Scripting' started by PachiGG, Apr 10, 2017.

  1. PachiGG

    PachiGG

    Joined:
    Apr 1, 2017
    Posts:
    239
    hi how can i save the coins when u close and open my app, and too have the same valuie on 2 scenes
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Depends...how secure do you need your coins to be? You have playerprefs, text files, backend services, etc.
     
  3. PachiGG

    PachiGG

    Joined:
    Apr 1, 2017
    Posts:
    239
    difference between playerpreft and text files? i dont need secure is my first project
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Mostly where they are saved and how you access the data. I would say for your first project, you could do playerprefs fairly easy than. If all you want to do is save out coins.
     
  5. PachiGG

    PachiGG

    Joined:
    Apr 1, 2017
    Posts:
    239
    yea i want to save the coins between 2 scenes andd when u reopen the app for a cooin shop, can u send me some link of help how to use??
     
  6. PachiGG

    PachiGG

    Joined:
    Apr 1, 2017
    Posts:
    239
    i did this
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Playersave : MonoBehaviour
    7. {
    8.  
    9.  
    10.  
    11.     void GetInt()
    12.     {
    13.         GameController.instance.Updateco();
    14.         GameController.coin = PlayerPrefs.GetInt("score", GameController.coin);
    15.         PlayerPrefs.Save();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         GameController.instance.Updateco();
    22.         PlayerPrefs.SetInt("Score", GameController.coin);
    23.         PlayerPrefs.Save();
    24.     }
    25.     private void OnApplicationQuit()
    26.     {
    27.         {
    28.             PlayerPrefs.SetInt("Score", GameController.coin);
    29.             PlayerPrefs.Save();
    30.         }
    31.     }
    32.     void Awake()
    33.     {
    34.         PlayerPrefs.GetInt("Score", GameController.coin);
    35.  
    36.     }
    37. }
    38.  
    39. [CODE]
    40. noot working xdd
     
  7. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    If you don't need secure then PlayerPrefs is your easiest option. I'd recommend saving the coin value after every major coin spend and coin gain event (buying an item, after a level). This will reduce the pain for players if your app crashes for any reason.
     
  8. PachiGG

    PachiGG

    Joined:
    Apr 1, 2017
    Posts:
    239
    yea anywya that code is not working xdd, so i add
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]    GameController.instance.Updateco();
    4. [*]        [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=PlayerPrefs']PlayerPrefs[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=SetInt']SetInt[/URL]("Score", GameController.coin);
    5. [*]        [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=PlayerPrefs']PlayerPrefs[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Save']Save[/URL]();
    6. [*][CODE]
    7. [*]in the coins and when u go to menu ??
    8. [/LIST]
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    You should generally not save playerprefs in an update like that. It does nothing for you and just keeps writing to the device over and over. Only save if they spend or earn coins.

    Your GetInt calls are incorrect. Take a look at the PlayerPref docs for that. You need to assign it to an int value, not do it the way you have written.