ouzdmrkl
ouzdmrkl

Reputation: 36

(UNITY) My JSON file is not loading on start

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");
    }
}

Upvotes: 0

Views: 280

Answers (1)

derHugo
derHugo

Reputation: 90833

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.

Upvotes: 1

Related Questions