Unity 保存元素数组为“null”

问题描述

我在 Unity 中执行保存和加载功能,我正在学习互联网上的一个教程,一切正常,除了保存我拥有的库存。所以我有一个 ItemSlot 脚本,它在创建时在库存中设置为 ItemSlot,这个 ItemSlot 有里面的项目,然后计算那里有多少项目,我在 PlayerData 脚本中创建了这些 ItemSlots 的数组。我会在它被调用的那一行里放一些代码(所以当玩家保存游戏然后它实际上保存游戏等等),一切都从 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,InvUI);

    ThatSavyThingy.Serialize(streamyThingy,Data);
    streamyThingy.Close();

}

然后是玩家数据:

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,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 数组,我尝试通过其他方式,我尝试询问我的朋友,这里也是里面的错误信息PlayerData 中的 SaveInventory 函数

Object reference not set to an instance of an object

我很乐意提供任何帮助,因为我在过去 2 天里一直在努力解决这个问题,但我无法解决这个问题(其他一切都在保存,例如 Health、MaxHealth 等,只有 ItemSlot 数组可以不被保存),我希望堆栈溢出不会关闭这个问题,因为每次它都会这样做,当我在这里查看其他问题时,它们不起作用

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...