How to save prefab reference in ScriptableObject?
Posted: Mon Sep 14, 2020 7:35 am
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)
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);
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)
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);