How to save prefab reference in ScriptableObject?

Discussion and help for Easy Save 3
Post Reply
kendemu
Posts: 1
Joined: Mon Sep 14, 2020 7:13 am

How to save prefab reference in ScriptableObject?

Post by kendemu »

Hello, I am making a sandbox game.
I am trying to save primitive shapes prefab in a ScriptableObject, but the prefab reference is linked to an empty object when loading. The other values(Vector3/List) are loaded properly. The ScriptableObject prefab reference works in runtime, but it becomes an empty object when loading.

Settings : JSON, load by reference, scene manager enabled, ES3prefab enabled(CubeParts which I want to load)
Image
Image

Here's the code chunks:

---CurrentInventory.cs---
public class CurrentInventory : ScriptableObject
{
[SerializeField]
public List<Item> items;
}

--Item.cs---
[System.Serializable]
public class Item : ICloneable
{
[SerializeField]
public int objectID;
public string objectName;
public GameObject prefab;
public Sprite snapshot;
public float mass;
public Color color;
public Vector3 position;
public Quaternion rotation;
public int runtimeIdx;
[SerializeField]
public List<Connection> connectedObjects = new List<Connection>();



public object Clone() {
return this.MemberwiseClone();
}
}
---Inventory Data(Blueprint of the inventory)---
public class InventoryData : ScriptableObject
{

[SerializeField]
public List<Item> items = new List<Item>();
}

---Adding Inventory---
void SaveItems() {
Item savedItem = (Item)inventoryData.items[itemObjectID].Clone();
savedItem.position = currentItem.transform.position;
savedItem.rotation = currentItem.transform.rotation;
savedItem.runtimeIdx = runtimeIdx;
sourceSnap.runtimeIdx = runtimeIdx;
if (isSnap)
{
savedItem.connectedObjects = new List<Connection>();
Connection connection = (Connection)new Connection().Clone();
connection.idx = targetSnap.runtimeIdx;
connection.position = targetSnap.snapPoints[targetIdx].transform.position;
savedItem.connectedObjects.Add(connection);
}
currentInventory.items.Add(savedItem);
runtimeIdx++;
}

---Save/Load Code---
ES3.Save<CurrentInventory>("inventory", currentInventory);
ES3.Load<CurrentInventory>("inventory", currentInventory);

User avatar
Joel
Moodkie Staff
Posts: 2973
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to save prefab reference in ScriptableObject?

Post by Joel »

Hi there, and thanks for getting in contact.

It's not possible for me to tell what is happening from what you've sent me. Please could you create a new project with a basic scene which replicates your issue and private message it to me?

All the best,
Joel
Joel @ Moodkie Interactive
Twitter - Unity

Post Reply