i was doing save and load functions in Unity, i was following one tutorial on internet and everything was working, except saving Inventory i had. So i have script for ItemSlot that is set to ItemSlot in inventory when created, this ItemSlot have the Item thats inside it and then Count of how many item are there, i created an Array of these ItemSlots inside PlayerData script. i will put some code here in the line that it is called (so when the player save the game then it actually save the game etc etc), everything is starting in GameManager:
if(Input.GetKeyDown(KeyCode.F6)) {
Save();
}
if(Input.GetKeyDown(KeyCode.F7)) {
Load();
}
Save, and load Function:
//Function that Save our Game progress
public void Save() {
//Call Save Function
SaveAndLoad.Save(health, sprinting, InvUI);
}
//Load the Game Progress
public void Load() {
//create PlayerData variable and set it to what Load return
PlayerData data = SaveAndLoad.Load();
//set variables to Health
health.health = data.Health;
health.maxHealth = data.MaxHealth;
//set variables to Sprinting
sprinting.Stamina = data.Stamina;
sprinting.MaxStamina = data.MaxStamina;
sprinting.MovementSpeedWhileSprinting = data.MovementSpeedWhileSprinting;
sprinting.MovementSpeedWhileWalking = data.MovementSpeedWhileWalking;
sprinting.StaminaDownWhileSprinting = data.StaminaDownWhileSprinting;
sprinting.StaminaAddWhileSprinting = data.StaminaAddWhileSprinting;
//set variables to Inventory
Inventory.instance.InvUI.itemSlotList.Clear();
for(int i = 0; i < data.InventorySlots.Length; i++) {
Inventory.instance.InvUI.itemSlotList.Add(data.InventorySlots[i]);
}
}
Save function inside SaveAndLoad:
public static void Save(Health health, Sprinting sprinting, InventoryUI InvUI) {
BinaryFormatter ThatSavyThingy = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.AutoBox";
FileStream streamyThingy = new FileStream(path, FileMode.CreateNew);
PlayerData Data = new PlayerData(health, sprinting, InvUI);
ThatSavyThingy.Serialize(streamyThingy, Data);
streamyThingy.Close();
}
and then PlayerData:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData {
//make some variables to make things better
public InventoryUI InvUI;
//variables from Health
public float Health;
public float MaxHealth;
//variables from Sprinting
public float Stamina;
public float MaxStamina;
public float MovementSpeedWhileSprinting;
public float MovementSpeedWhileWalking;
public float StaminaDownWhileSprinting;
public float StaminaAddWhileSprinting;
//variables from Inventory
public ItemSlot[] InventorySlots;
//Constructor
public PlayerData(Health health, Sprinting sprinting, InventoryUI InvUI) {
Health = health.health;
MaxHealth = health.maxHealth;
Stamina = sprinting.Stamina;
MaxStamina = sprinting.MaxStamina;
MovementSpeedWhileSprinting = sprinting.MovementSpeedWhileSprinting;
MovementSpeedWhileWalking = sprinting.MovementSpeedWhileWalking;
StaminaDownWhileSprinting = sprinting.StaminaDownWhileSprinting;
StaminaAddWhileSprinting = sprinting.StaminaAddWhileSprinting;
this.InvUI = InvUI;
SaveInventory();
}
public void SaveInventory() {
for(int i = 0; i < InvUI.itemSlotList.Count; i++) {
InventorySlots[i] = InvUI.LoopThroughItemSlots(); //the error is on this line
}
}
}
and lastly LoopThroughItemSlots function:
public ItemSlot LoopThroughItemSlots() {
foreach(ItemSlot slot in itemSlotList) {
return slot;
break;
}
return null;
}
i have literally tried anything, i tried to just loop through itemSlotList and then assign that slot to the Array of InventorySlots, i tried going through other ways, i tried asking my friend, also here is the error message on the line inside the SaveInventory Function in the PlayerData:
Object reference not set to an instance of an object
i will be happy for any help because i'm trying to fix this for like past 2 days and i can't the solution to this(also everything else is saving, like Health, MaxHealth etc, just the ItemSlot array can't be saved), i hope that Stack overflow doesn't close this question because everytime it does that, and when i look on the other questions here, they don't work