0
\$\begingroup\$

I've been working on an RPG videogame, and I have been trying to implement a Save button into my game, which is supposed to save the player's itemAmount variable, which is in another script, called ItemInformation. I retrieve the itemAmount variable at Start() by getting the script from the itemHolder GameObject. I plan to use this script in multiple GameObjects, because I use the same variable (itemAmount) with different values in different GameObjects. For some reason, I don't receive any errors or warnings in the Console. Here is my script to the Saving System:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class InventorySave : MonoBehaviour
{
    // Public variables for inventory and health
    private GameObject itemHolder;
    private ItemInformation myItemAmount;

    // Initiates before the first frame
    void Start()
    {
        // Gets components for the player variables
        itemHolder = transform.gameObject;
        myItemAmount = itemHolder.GetComponent<ItemInformation>();
    }

    // Function that will be called when player saves game from pause menu
    public void SaveGame()
    {
        // Creates file to store variables
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/SaveData.dat");
        SaveData data = new SaveData();

        // Gets the player data
        data.itemSave = myItemAmount.itemAmount;

        // Writes player data to file and encrypts it
        bf.Serialize(file, data);
        file.Close();
    }
}

// Holds data that is to be saved
[Serializable]
class SaveData
{
    // Variables for save file
    public int itemSave;
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Does the file at Application.persistentDataPath + "/SaveData.dat" is being created ? \$\endgroup\$ Commented Jan 17, 2021 at 20:22
  • \$\begingroup\$ @ÇağatayIŞIK I don’t know, because I don’t know where it is stored. \$\endgroup\$ Commented Jan 17, 2021 at 20:23
  • 1
    \$\begingroup\$ Use Debug.log to log that persistant data path and then check if it creates that file in there. For windows it's location is -> C/Users/{username}/AppData/LocalLow/{CompanyName} \$\endgroup\$ Commented Jan 17, 2021 at 20:25
  • \$\begingroup\$ Why are you grabbing the itemAmount on Start and not right before saving? If you increase or decrease the value, the change won't be noticed. You as well only save the amount without what item it is, so all your file will contain is a number. And last, you save only a single item. Since the name of the file is hard codes, each item should overwrite the saves before. \$\endgroup\$
    – Zibelas
    Commented Jan 17, 2021 at 21:55

1 Answer 1

0
\$\begingroup\$

Don’t worry I fixed the problem. I just forgot to implement a Load System!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .