如何解决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 举报,一经查实,本站将立刻删除。