Page 1 of 1

How to save/load a list of instances (GameObject) in an ES3ComponentType?

Posted: Wed Apr 03, 2024 12:40 pm
by gabrieljacintho
Hello, :D

I tried to create an ES3ComponentType that would save a list of GameObjects (instances) and load it, but I noticed that after I close and open the game again the list is loaded with null spaces.

And yes, prefabs have the ES3Prefab component.

Is it possible to do what I'm trying to do? My goal is to load an ObjectFactory that is inside an instance instantiated by another ObjectFactory.

Just for explanation purposes, I have an ObjectFactory of breakable crates that when broken, instantiate a GameObject.

Thank you for your attention,
Gabriel Jacintho

Re: How to save/load a list of instances (GameObject) in an ES3ComponentType?

Posted: Wed Apr 03, 2024 1:46 pm
by Joel
Hi there,

If you have a field which is a List<GameObject> you should just be able to navigate to your Component in the Easy Save 3 > Types window and select that field, which will automatically create the ES3Type. You shouldn't need to create your own ES3ComponentType.

All the best,
Joel

Re: How to save/load a list of instances (GameObject) in an ES3ComponentType?

Posted: Wed Apr 03, 2024 6:33 pm
by gabrieljacintho
Hello Joel, nice to meet you and thanks for your fast response!

I did exactly what you said and the script is still receiving a list with null items :cry:
ES3UserType_Spawner.cs
(1.79 KiB) Downloaded 36 times
When I save the game and load again it works, but if a close the game and reopen, the load return a list with null items.

Note: The game objects are in the DontDestroyOnLoad scene, but I tested in the game scene and also don't worked.

Do you have any suggestion about how to fix it?

Re: How to save/load a list of instances (GameObject) in an ES3ComponentType?

Posted: Thu Apr 04, 2024 8:47 am
by Joel
Hi there,

Fields of UnityEngine.Object type (or Collections of UnityEngine.Object type) will be saved by reference, not by value.

In your case you would either need to save your List of GameObjects using a separate ES3.Save call to ensure that they're saved by value and reference, or you would need to provide an ES3Settings object which specifies that you want fields to be saved by reference and value. I.e.

Code: Select all

var settings = new ES3Settings();
settings.memberReferenceMode = ES3.ReferenceMode.ByRefAndValue;
ES3.Save("key", yourSpawner, settings);
All the best,
Joel

Re: How to save/load a list of instances (GameObject) in an ES3ComponentType?

Posted: Thu Apr 04, 2024 5:49 pm
by gabrieljacintho
Okay, thank you for the explanation! My goal was to make this without a save key, but I made with ES3.Save and it worked! :D