ParentClass Data cannot be saved

Discussion and help for Easy Save 3
Post Reply
a33366
Posts: 2
Joined: Wed Jul 05, 2023 12:05 pm

ParentClass Data cannot be saved

Post by a33366 »

Code: Select all

public class ParentClass
{
    [ES3Serializable]
    public int myInt = 123;
}
public class ChildClass : ParentClass
{
    public string myString = "myStr";
}
public class Test : MonoBehaviour
{
    void Start()
    {
        ParentClass obj = new ChildClass();
        obj.myInt = 456;
        ES3.Save("obj", obj);
    }
    	// result
    	"obj" : {
		"__type" : "ParentClass,Assembly-CSharp",
		"value" : {
			"__type" : "ChildClass,Assembly-CSharp",
			"myString" : "myStr"
		}
	}
    
my json Why is there no myInt field ? Do I need to keep them separately?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: ParentClass Data cannot be saved

Post by Joel »

Hi there,

I've tested this at my end and it appears to be working fine. Are you on the latest version of Easy Save?

I used the following script:

Code: Select all

using UnityEngine;

public class InheritenceTest : MonoBehaviour
{
    void Start()
    {
        ParentClass obj = new ChildClass();
        obj.myInt = 456;
        ES3.Save("obj", obj);
        var newObj = ES3.Load<ParentClass>("obj");
        Debug.Assert(newObj.myInt == 456);
    }
}

public class ParentClass
{
    [ES3Serializable]
    public int myInt = 123;
}
public class ChildClass : ParentClass
{
    public string myString = "myStr";
}
The data stored in the file is:

Code: Select all

{
	"obj" : {
		"__type" : "ParentClass,Assembly-CSharp",
		"value" : {
			"__type" : "ChildClass,Assembly-CSharp",
			"myString" : "myStr",
			"myInt" : 456
		}
	}
}
If you're on the latest version you might want to try deleting the package from the package cache and then re-downloading in case an issue has occurred at Unity's end and it's not imported the complete package, meaning old files will remain:

https://docs.unity3d.com/Manual/upm-del ... cache.html

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
a33366
Posts: 2
Joined: Wed Jul 05, 2023 12:05 pm

Re: ParentClass Data cannot be saved

Post by a33366 »

Thanks for reimporting to fix the problem.
Post Reply