0

I am making a basic shop system includes car name, price and buyable. I am saving data to a text file. When I replay, I can't get the last variables I've changed. But, if I refresh at Editor by CTRL+R and start the game, variables are loading correct. What am I doing wrong? I am new at storage and JSON issues. Thanks for answers...

Here is the code:

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

public class GameHandler : MonoBehaviour
{
    [Serializable]
    public class Car
    {
        public string name;
        public int price;
        public bool unlocked;
    }

    [Serializable]
    public class CarList{
        public Car[] cars;
    }

    private Car cars = new Car();
    public CarList myCars = new CarList();

    //-------
    public int chosenCar;

    //-------
    public TextAsset CARS;

    //-------
    private void Start() {
        GetFromJSON();
    }

    private void Update() {
        
        if(Input.GetKeyDown(KeyCode.L))
            GetFromJSON();

        if(Input.GetKeyDown(KeyCode.S))
            SaveToJSON();
    }

    public void ChangeCarIndex(int a){
        chosenCar = a;
    }
    //This is a button func.
    public void ChangePrice(int a){

        myCars.cars[chosenCar].price = a;
        SaveToJSON();
    }

    public void GetCarName(){

        Debug.Log(myCars.cars[chosenCar].name);
    }

    public void GetCarPrice(){

        Debug.Log(myCars.cars[chosenCar].price);
    }


    public void SaveToJSON(){
        
        string jsOutput = JsonUtility.ToJson(myCars);

        File.WriteAllText(Application.dataPath + "/CARS.txt", jsOutput);

        Debug.Log("SaveToJSON() called");
    }

    public void GetFromJSON(){

        myCars = JsonUtility.FromJson<CarList>(CARS.text);
        
        Debug.Log("GetFromJSON() called");
    }
}

1 Answer 1

1

When running this in the editor try to add

public void SaveToJSON()
{
    string jsOutput = JsonUtility.ToJson(myCars);

    File.WriteAllText(Application.dataPath + "/CARS.txt", jsOutput);

    Debug.Log("SaveToJSON() called");

#if UNITY_EDITOR
    UnityEditor.AssetDatabase.Refresh();
#endif
}

Though actually this isn't really necessary! Afaik you could also simply set the text of the textasset:

public void SaveToJSON()
{
    string jsOutput = JsonUtility.ToJson(myCars);

    CARS.text = jsOutput;

    Debug.Log("SaveToJSON() called");
}

BUT NOTE:

in general note that this makes only sense in the editor itself.

If you target to do this in an actually later built application you would rather go via a file in Application.persistentDataPath. Problem with that though: The data can easily be seen and edited by the user. So if this is anything sensitive you will need to go for a central database server with user login.

Sign up to request clarification or add additional context in comments.

2 Comments

My problem solved, thank you...
@ouzdmrkl Glad to help :) Note btw the update at the bottom! This is only for the Editor but if your goal is actually store and load this from the target device you'll need to rather use a persistent file stored in the Application.persistentataPath ... problem with that in particular if this is something about achievements or user progress: The persistent data is viewable and editable for the user => you would need some kind of encryption etc otherwise the data can easily be changed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.