0

我在 Unity 中执行保存和加载功能,我正在关注互联网上的一个教程,一切正常,除了保存我的库存。所以我有一个 ItemSlot 的脚本,它在创建时设置为库存中的 ItemSlot,这个 ItemSlot 有里面的项目,然后计算有多少项目,我在 PlayerData 脚本中创建了这些 ItemSlot 的数组。我将在调用它的行中放一些代码(所以当玩家保存游戏然后它实际上保存游戏等),一切都在 GameManager 中开始:

if(Input.GetKeyDown(KeyCode.F6)) {

        Save();

    }

    if(Input.GetKeyDown(KeyCode.F7)) {

        Load();

    }

保存和加载功能:

//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]);

    }

}

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();

}

然后是 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

    }

}

}

最后是 LoopThroughItemSlots 函数:

public ItemSlot LoopThroughItemSlots() {

    foreach(ItemSlot slot in itemSlotList) {

        return slot;
        break;

    }

    return null;

}

我确实尝试过任何东西,我尝试循环遍历 itemSlotList,然后将该插槽分配给 InventorySlots 数组,我尝试通过其他方式,我尝试询问我的朋友,这也是 SaveInventory 函数内的行上的错误消息在播放器数据中:

Object reference not set to an instance of an object

我会很高兴得到任何帮助,因为我在过去 2 天里一直在尝试解决这个问题,但我无法解决这个问题(其他所有东西都在保存,比如 Health、MaxHealth 等,只是 ItemSlot 数组不能保存),我希望堆栈溢出不会关闭这个问题,因为每次它都会这样做,当我在这里查看其他问题时,它们不起作用

4

0 回答 0