Help Needed: Issue with Items Not Being Picked Up After Save and Load in Unity,Thanks !

Hi everyone,

I encountered an issue with items after saving and loading in my game, and I need some help.

I am trying to save itemStateData at the end of each day by calling the Save() method, which saves the data to a JSON file. When the game loads, it retrieves the item states from the JSON file. However, after loading, the items on the ground can no longer be picked up.

Here is the relevant code:

ItemStateData class, defining which data to save:

using UnityEngine;

[System.Serializable]
public class ItemStateData
{
public string itemName;
public Sprite icon;
public Vector3 position;
public bool isCollected;
public ItemStateData(Item item)
{
if (item != null)
{
itemName = item.data.itemName;
icon = item.data.icon;
position = item.transform.position;
isCollected = item.isCollected;
}
}

}
ItemManager class, defining methods to save and load itemStateData:

public List SaveItemStateData()
{
List itemStateData = new List();
foreach (var item in items)
{
if (item != null&& !item.isCollected)
{
itemStateData.Add(new ItemStateData(item));
}
}
Debug.Log($“Saved {itemStateData.Count} items.”);
return itemStateData;
}

public void LoadItemStateData(List itemStateData)
{
Debug.Log(“Loading item state data…”);
foreach (var item in items)
{
if (item != null && item.gameObject.scene.IsValid())
{
Destroy(item.gameObject);
Debug.Log($“Destroyed item: {item.data.itemName}”);
}
}
items.Clear();
foreach (var data in itemStateData)
{
if (data.itemName != null&& !data.isCollected)
{
Item itemPrefab = GetItemByName(data.itemName);
if (itemPrefab != null)
{
Item item = Instantiate(itemPrefab, data.position, Quaternion.identity);
item.data.itemName = data.itemName;
item.data.icon = data.icon;
item.isCollected = data.isCollected;
item.gameObject.SetActive(!item.isCollected);

item.rb2d = item.GetComponent();
if (item.rb2d == null)
{
item.rb2d = item.gameObject.AddComponent();
}
Collider2D collider = item.GetComponent();
if (collider == null)
{
collider = item.gameObject.AddComponent();
}
collider.isTrigger = true;

Collectable collectable = item.GetComponent();
if (collectable == null)
{
collectable = item.gameObject.AddComponent();
}
collectable.item = item;

Debug.Log("Loaded item: {data.itemName}, isCollected: {data.isCollected}"); items.Add(item); AddItem(item); } } } Debug.Log(“Loaded {itemStateData.Count} items.”);
}

public void InitializeForNewGame()
{
items.Clear();
nameToItemDict.Clear();
InitializeItems();
}
SaveLoadSystem class, defining methods to save and load data using JSON:

using System.IO;

using UnityEngine;

public class SaveLoadSystem

{

private static string savePath = Path.Combine(Application.persistentDataPath, “gameData.json”);

public static void SaveGame(GameData data)

{

string json = JsonUtility.ToJson(data, true);

File.WriteAllText(savePath, json);

Debug.Log("Game saved to " + savePath);

}

public static GameData LoadGame()

{

if (File.Exists(savePath))

{

string json = File.ReadAllText(savePath);

if (!string.IsNullOrEmpty(json))

{

Debug.Log("Game loaded from " + savePath);

return JsonUtility.FromJson(json);

}

}

Debug.LogWarning(“No save file found or save file is empty. Starting new game.”);

return new GameData(); // Return a new game data object if no save file is found or content is invalid

}

public static void NewGame()

{

GameData newGameData = new GameData();

SaveGame(newGameData);

}

public static bool SaveFileExists()

{

return File.Exists(savePath);

}

}

Can anyone help me figure out why the items on the ground cannot be picked up after loading the game? Thanks a lot!

Sounds like it is time to start debugging!

I would start your debugging by looking closely at what governs pick-ability of things.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

ALSO, for future reference, if you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

Thank you so much ! I will try to debug more to find the problem . And thanks for “Code Tag” suggestion !