Answer the question
In order to leave comments, you need to log in
How to save coins and update them?
I need to save the coins, but when changing the location or restarting the level, the coins are saved once and are not updated, I don’t know how to fix this, please help. I do everything in Unity. here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
public class money : MonoBehaviour
{ public GameObject Money;
private Save sv = new Save ();
private string path;
public static int score = 0;
private void OnTriggerEnter2D(Collider2D collision)
{ score++;
Destroy(Money);
}
// Start is called before the first frame update
private void Start()
{
path = Path.Combine(Application.dataPath, "Save.json");
if (File.Exists(path)) {
sv = JsonUtility.FromJson(File.ReadAllText(path));
}
}
private void OnApplicationQuit()
{sv.score = score;
File.WriteAllText(path, JsonUtility.ToJson(sv));
}
// Update is called once per frame
void Update()
{
}
[Serializable]
public class Save {
public int score;}
}
Answer the question
In order to leave comments, you need to log in
Alternatively, you can try writing them in PlayerPrefs. But, according to your description, it is not entirely clear when they should be saved. Between launches of levels or between launches of the application itself?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question