Hello everyone, first time using the forums so bare with me here.
I’m making a small game in Unity v.5.6.3p2 and the base concept is that the player has a inventory on which i store variables such as health, money etc etc.
The error i get is
and i understand the error and all but i’m not sure on how i would solve it and that is why i’m in hope that one of you all can help me.
This is my Inventory Script (Very simple atm but more will be added there later)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
private Player _Player;
public GameObject Player;
public float Health = 0.0f;
public int Gold = 0;
public int Stone = 0;
public int Wood = 0;
public int Emerald = 0;
public int Ruby = 0;
public int Sapphire = 0;
private bool IsBuying = false;
private int PriceOfLandINT;
public Text myText;
private void Update() //Checks if player is in buy menu and changes the myText to next message
{
IsBuying = Player.GetComponent<Player>().IsBuying;
if (IsBuying == true)
{
PriceOfLandINT = Player.GetComponent<Player>().PriceOfLandINT;
myText.text = "Current gold : "+ Gold +
"\n Map cost :"+ PriceOfLandINT;
}
else
{
UIText();
}
}
public void UIText() //This is always visable unless player is in the buy menu
{
myText.text = "Health :" + Health +
"\n Gold :" + Gold +
"\n Stone :" + Stone +
"\n Wood :" + Wood +
"\n Emerald :" + Emerald +
"\n Ruby :" + Ruby +
"\n Sapphire :" + Sapphire +
"";
}
}
and here is my Save/load script
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GameData
{
public static float Health;
public static int Gold;
public static int Stone;
public static int Wood;
public static int Emerald;
public static int Ruby;
public static int Sapphire;
public static int PriceOfLandInt;
public static int xp;
public static int Level;
public static GameObject[] StartLand;
public static GameObject[] BuyableMaps;
}
public class SaveLoad
{
private Inventory _Inventory;
public void Save()
{
GameData.Health = _Inventory.Health;
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fs = new FileStream("gamesave.bin", FileMode.Create, FileAccess.Write))
{
binaryFormatter.Serialize(fs, GameData.Health);
binaryFormatter.Serialize(fs, GameData.Gold);
binaryFormatter.Serialize(fs, GameData.Stone);
binaryFormatter.Serialize(fs, GameData.Wood);
binaryFormatter.Serialize(fs, GameData.Emerald);
binaryFormatter.Serialize(fs, GameData.Ruby);
binaryFormatter.Serialize(fs, GameData.Sapphire);
binaryFormatter.Serialize(fs, GameData.PriceOfLandInt);
binaryFormatter.Serialize(fs, GameData.xp);
binaryFormatter.Serialize(fs, GameData.Level);
}
}
public static void Load()
{
if(!File.Exists("gamesave.bin"))
{
return;
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fs = new FileStream("gamesave.bin", FileMode.Open, FileAccess.Read))
{
GameData.Health = (float)binaryFormatter.Deserialize(fs);
GameData.Gold = (int)binaryFormatter.Deserialize(fs);
GameData.Stone = (int)binaryFormatter.Deserialize(fs);
GameData.Wood = (int)binaryFormatter.Deserialize(fs);
GameData.Emerald = (int)binaryFormatter.Deserialize(fs);
GameData.Ruby = (int)binaryFormatter.Deserialize(fs);
GameData.Sapphire = (int)binaryFormatter.Deserialize(fs);
GameData.PriceOfLandInt = (int)binaryFormatter.Deserialize(fs);
GameData.xp = (int)binaryFormatter.Deserialize(fs);
GameData.Level = (int)binaryFormatter.Deserialize(fs);
}
}
}
and this is how i call it ingame (this will also be changed later but im using this to test if it works)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace Assets.Scripts
{
public class SaveManager : MonoBehaviour
{
void Start()
{
SaveLoad.Save();
SaveLoad.Load();
Debug.Log(GameData.Health);
}
}
}
Thanks in advance for any help and im sorry if there is some grammar mistakes. English is not my main language
Best Regards
Lunix
ps, if you need any more info just ask and i will try to answer it as best as i can