How do I Save,Load a class with a reference to MonoBehaviour?

Discussion and help for Easy Save 3
Post Reply
KBTIT
Posts: 14
Joined: Mon Jul 17, 2023 5:24 am

How do I Save,Load a class with a reference to MonoBehaviour?

Post by KBTIT »

How do I Save,Load a class with a reference to MonoBehaviour?

https://docs.moodkie.com/easy-save-3/es ... -is-saved/
saw this and ran "EasySave3TypesWindowCreating an ES3Type" and found that the following statement would save it.

Code: Select all

[ES3Serializable]
public MonoClass monoClass { get; set; } = null;
However, what is saved is only the number and not the original class load, as shown below.

Code: Select all

"monoClass" : {
	"_ES3Ref" : "4095535722558072592"
},
I am using translation software, so it is possible that I am not understanding the documentation correctly, but I am very confused.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How do I Save,Load a class with a reference to MonoBehaviour?

Post by Joel »

Hi there,

Code: Select all

"monoClass" : {
	"_ES3Ref" : "4095535722558072592"
},
This is a reference to a MonoBehaviour. The number you are seeing in the reference ID, so it will be saved and loaded by reference.

If you want the fields of the MonoBehaviour to be saved then you should save it by reference and by value. The easiest way to do this would be to save the MonoBehaviour directly with a separate ES3.Save call. Alternatively you can go to Tools > Easy Save 3 > Settings > Advanced Settings, and change Serialize Unity Object fields to By Ref and Value. Be aware that this will save all of the fields by reference and value.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
KBTIT
Posts: 14
Joined: Mon Jul 17, 2023 5:24 am

Re: How do I Save,Load a class with a reference to MonoBehaviour?

Post by KBTIT »

Hi, could you please tell me how to achieve the following steps. Or please give me the URL of the documentation.
"would be to save the MonoBehaviour directly with a separate ES3.Save call."

The first method seems to be better, because "Serialize Unity Object fields to By Ref and Value" brings the following error when loaded

Code: Select all

NullReferenceException: Object reference not set to an instance of an object
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How do I Save,Load a class with a reference to MonoBehaviour?

Post by Joel »

Hi there,

You would do a separate ES3.Save and ES3.Load call. I.e. instead of saving a class which contains a reference to your MonoBehaviour, save the MonoBehaviour itself.

Code: Select all

//To save
ES3.Save("monoClass", monoClass);

Code: Select all

//To load
monoClass = ES3.Load<MonoClass>("monoClass");
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
KBTIT
Posts: 14
Joined: Mon Jul 17, 2023 5:24 am

Re: How do I Save,Load a class with a reference to MonoBehaviour?

Post by KBTIT »

Thank you very much.
I have confirmed that I can save the file that way.

However, this method breaks references in the class.

Do I have to fix that part manually?
A class that inherits MonoBehaviour placed on all maps and a class that does not inherit MonoBehaviour that references it.

It would be useful to be able to save and load these all together.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How do I Save,Load a class with a reference to MonoBehaviour?

Post by Joel »

Hi there,

Please could you create a new project with a very simple scene which replicates your issue and private message it to me with instructions so I can see what you mean?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
KBTIT
Posts: 14
Joined: Mon Jul 17, 2023 5:24 am

Re: How do I Save,Load a class with a reference to MonoBehaviour?

Post by KBTIT »

Sorry for the delay in replying.
I am currently reviewing this post.

I will let you know if there are any problems.
Post Reply