how to store coins after death?

hi in my game when the player died the coins restore to 0 idk how to make “store” and make it “collectionable”
soome help ? :frowning:

For simple games you can do PlayerPrefs, but if you need more features and a more complex game then you should do some kind of save/load system

You should first look into data persistence to solve your coin problem. It will help you save/load data (coins in your case). This is a decent video to get you started with this concept (its what I used to learn how to save/load data):

I’m not to sure on exactly how it functions, but here is an official Unity tutorial on data persistence: Persistence: Saving and Loading Data - Unity Learn

What does the code look like ? ** I think the OP is asking (originally) how to just keep the score between "deaths’ not loads.
If you post the code for the death/restart stuff, that would help.

ty but dont work, i did this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Playersave : MonoBehaviour {

   


   

    // Update is called once per frame
    void Update()
    {
        PlayerPrefs.GetInt("Score", GameController.coin);

    }
}
[CODE]


idk what put in the string because is a int the value that i want to store, and gamecontroller.coin is

[CODE]
    public void Coin()
    {
        coin++;
        cointext.text ="" + coin;
        SoundSystem.instance.PlayCoin();
}
[CODE]

ty but i dont speak spanish :frowning: (i speka spanish but for listening in english i am so bad)

Do you only want this score between “life & death” or between opening & closing the game?

opening and closing, are coins that have to stay for the shop :), the system work fine after u pick 1 coin but like i say when u die and touch to restart the coins are gonne :frowning:

what code u want? the code of the score text script or the gamecontroller? (in gamecontroller methods like collectcoin death)

Okay, well those are 2 different save issues, for sure. With PlayerPrefs and saving between opening and closing, you also have : Save and Load. For between death and restart, there must be a part of your code that is resetting the score (either at the start or the end of the ‘round’).

Yep, anything related to death or restarting the match (or even simpler still if you search the code for “coin = 0” you’ll probably find where it’s resetting :slight_smile:

ok

"coins"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Coin : MonoBehaviour {

    public int columnPoolSize = 4;
    public GameObject columnPrefab;

    public float columnMin = -2.9f;
    public float columnMax = 1.4f;
    private float spawnXPosition = 16.8f;

    private GameObject[] columns;
    private Vector2 objectPoolPosition = new Vector2(-14, 0);

    private float timeSinceLastSpawned;
    public float spawRate;

    private int currentColumn;

    // Use this for initialization
    void Start()
    {
        columns = new GameObject[columnPoolSize];
        for (int i = 0; i < columnPoolSize; i++)
        {
            columns[i] = Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
        }

        SpawnColumn();
    }

    // Update is called once per frame
    void Update()
    {
        timeSinceLastSpawned += Time.deltaTime;
        if (!GameController.instance.gameOver && timeSinceLastSpawned >= spawRate)
        {
            timeSinceLastSpawned = 0;
            SpawnColumn();
        }
    }

    void SpawnColumn()
    {
        float spawnYPosition = Random.Range(columnMin, columnMax);
        columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);

        currentColumn++;
        if (currentColumn >= columnPoolSize)
        {
            currentColumn = 0;
        }
    }
}


[CODE]

"coins score"
[CODE]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CoinsScore : MonoBehaviour
{

    public GameObject ban;
    private void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.CompareTag("Player"))
        {
            GameController.instance.Coin();
            Destroy(ban);
        }
    }
}
[CODE]

"gamecontroller"
[CODE]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameController : MonoBehaviour {

    public static GameController instance;

    public GameObject gameOverText;

    public bool gameOver;
    public float scrollSpeed = -1.5f;

    private int  score;
    public static int coin;
    public Text scoreText;
    public Text cointext;

    private void Awake()
    {
        if(GameController.instance == null)
        {
            GameController.instance = this;
        }else if(GameController.instance != this)
        {
            Destroy(gameObject);
            Debug.LogWarning("GameController ha sido instanciado por segunda vez. Esto no debería pasar.");
        }
      
    }


    public void Coin()
    {
        coin++;
        cointext.text ="" + coin;
        SoundSystem.instance.PlayCoin();


    }
    public void BirdScored()
    {
        if (gameOver) return;

        score++;
        scoreText.text = "Score: " + score;

    }
  
    public void BirdDie()
    {
        gameOverText.SetActive(true);
        gameOver = true;
    }

    private void OnDestroy()
    {
        if(GameController.instance == this)
        {
            GameController.instance = null;
        }
    }
}

[CODE]

"playersave"(do it from the info of here and dont work xd)
[CODE]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Playersave : MonoBehaviour {

  


  

    // Update is called once per frame
    void Update()
    {
        PlayerPrefs.GetInt("Score", GameController.coin);

    }
}

[CODE]

I didn’t see this post in my update alerts at first, so I didn’t know it was here.
I see a few things that could be improved, but for now I can’t see any code that restarts the game?
Do you have to actually stop the game and restart to play again? Or is there some other code that restarts?

here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class FlapToRestart : MonoBehaviour {

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }
}

[CODE]

and here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bird : MonoBehaviour {

    private bool isDead;
    private Rigidbody2D rb2d;
    private Animator anim;
    public float upForce = 200f;

    private RotateBird rotateBird;

    private void Awake()
    {
        rb2d = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        rotateBird = GetComponent<RotateBird>();
    }

    // Use this for initialization
    private void Start () {
       
    }

    // Update is called once per frame
    private void Update () {
        if (isDead) return;
       
        if (Input.GetMouseButtonDown(0))
        {
            rb2d.velocity = Vector2.zero;
            rb2d.AddForce(Vector2.up * upForce);
            anim.SetTrigger("Flap");
            SoundSystem.instance.PlayFlap();
        }        
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        isDead = true;
        anim.SetTrigger("Die");
        rotateBird.enabled = false;
        GameController.instance.BirdDie();
        rb2d.velocity = Vector2.zero;
        SoundSystem.instance.PlayHit();
    }
}


[CODE]

i did this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Playersave : MonoBehaviour
{




    // Update is called once per frame
    void Update()
    {
        GameController.instance.Updateco();
        PlayerPrefs.SetInt("Score", GameController.coin);
        PlayerPrefs.Save();
    }
    private void OnApplicationQuit()
    {
        {
            PlayerPrefs.SetInt("Score", GameController.coin);
            PlayerPrefs.Save();
        }
    }
    void Awake()
    {
        PlayerPrefs.GetInt("Score");

    }
}


[CODE]
still without saving the value after closing and opening the game

I see. Well, you needn’t run the save in an update loop. You do not need to call Save() in OnApplicationQuit, either (it automatically saves).
Using Awake to load is good.
You can call PlayerPrefs from anywhere. Call its SaveInt when you receive a coin, instead of update…
When you use “GetInt”, make sure you save the variable.

GameController.coin = PlayerPrefs.GetInt("score",0);
// the '0' there, means that is the default value if no entry is found, ie: the first time someone plays.

still without working :frowning:
the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Playersave : MonoBehaviour
{



    void GetInt()
    {
        GameController.instance.Updateco();
        GameController.coin = PlayerPrefs.GetInt("score", 0);
        PlayerPrefs.Save();
    }

    // Update is called once per frame
    void Update()
    {
        GameController.instance.Updateco();
        PlayerPrefs.SetInt("Score", GameController.coin);
        PlayerPrefs.Save();
    }
    private void OnApplicationQuit()
    {
        {
            PlayerPrefs.SetInt("Score", GameController.coin);
            PlayerPrefs.Save();
        }
    }
    void Awake()
    {
        PlayerPrefs.GetInt("Score");

    }
}

[CODE]
i putted in update too because if i dont put when u died the score go to 0 and only update if u pick up another coin so with the update say the score that u have without having to update pick uping another coin, but the functions of save when u close the app and re open still without working

if u want i can make a video with and without the update and ur code if u dont understand me xd (srry i speak spanish :smile:)

You could… I’m thinking, when you collect the coin, SetInt and Save().
when you restart the scene GetInt and print it out in Debug.Log
There is also no need to explicitly call SetInt & Save inside OnApplicationQuit.