ES3.Load gives extra Gameobjects

Discussion and help for Easy Save 3
Post Reply
golraz
Posts: 3
Joined: Sun May 12, 2019 6:06 pm

ES3.Load gives extra Gameobjects

Post by golraz »

Hello,

I run the following code each time my scene loads:

Code: Select all

    void Start()
    {
        if (SaveInformation.name == null)
        {
            SceneManager.LoadScene("Main menu");
        }

        if (ES3.KeyExists("Dynamics", "saves/"+SaveInformation.name+"/"+SceneManager.GetActiveScene().name+".s"))
        {
            foreach (GameObject child in GameObject.FindGameObjectsWithTag("Dynamic") )
            {
                Destroy(child);
            }
            allDynamicItems = ES3.Load<GameObject[]>("Dynamics", "saves/" + SaveInformation.name + "/" + SceneManager.GetActiveScene().name + ".s");


            foreach (GameObject item in allDynamicItems)
            {
                Instantiate(item);
            }
        }
    }


    void OnApplicationQuit()
    {
        ES3.Save<string>("scene", SceneManager.GetActiveScene().name, "saves/" + SaveInformation.name + ".s");
        ES3.DeleteDirectory("saves/" + SaveInformation.name + "/" + SceneManager.GetActiveScene().name + ".s");
        ES3.Save<GameObject[]>("Dynamics", GameObject.FindGameObjectsWithTag("Dynamic"), "saves/" + SaveInformation.name + "/" + SceneManager.GetActiveScene().name + ".s");
    }
When I enter a scene I check if there exists some data about the scene, if it does I delete all the gameobjects that are "dynamic" and load them again from my previous save (in case they have been moved or such). It works good the first time I load a scene (and the scene have stored data), but the second time I load a scene (after it has been saved 2 times) I get double gameobjects of all saved items.

If a scene have saved items and I load it the first time the items only get instantiated in the *Instantiate(item)* row. The second time I load the scene the items also get instantiated on the *allDynamicsItem = ES3.Load<GameObject[]>* row + on the Instantiate(item) row = I get double items in the scene ( and next time 4,8,16 and so on)

Do you have any idea why it works flawlessly the first time I load but not the second time?

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

Re: ES3.Load gives extra Gameobjects

Post by Joel »

Hi Alexander,

It's hard to tell from what you've said. But firstly, have you followed the Saving and Loading Prefab Instances instructions here?
https://docs.moodkie.com/easy-save-3/es ... s-prefabs/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
golraz
Posts: 3
Joined: Sun May 12, 2019 6:06 pm

Re: ES3.Load gives extra Gameobjects

Post by golraz »

Hi Joel and thanks for your reply.

I managed to get it to work as intended shortly after your answer. However I dont understand the part when I save a entire GameObject and when loaded the Mesh Filter is empty. I read another post about it https://moodkie.com/forum/viewtopic.php?f=13&t=1593
The way to go is " either save it separately using ES3.Save, or you can create an ES3Type file for the class containing your Mesh field and modify it so that it saves by value"
I really dont understand the ES3Type file and how to use it.

Is saving the mesh filter separately really a good way to go? I am saving all my GameObjects like this:

Code: Select all

 ES3.Save<GameObject[]>("dynamics", GameObject.FindGameObjectsWithTag("Dynamic"),"items.s");
and I retrieve them like this:

Code: Select all

ES3.Load<GameObject[]>("dynamics", "items.s");
Am I doing this completely wrong or am i missing something?

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

Re: ES3.Load gives extra Gameobjects

Post by Joel »

Hi there,

There can be many reasons why the MeshFilter might be null, so it's not possible to tell whether what is discussed in the other thread is related.

There's a couple of things I'll need to understand. Firstly, is your Mesh modified or procedurally-generated at runtime? If not, you don't need to save the Mesh itself, only a reference to it.

If the reference is null, it suggests that it's not in the reference manager. Are you loading it in the same scene as you saved it?

If your Mesh is only referenced by a prefab, you might want to try dragging the prefab into your scene and then setting it to inactive. This ensures that the reference manager can find a reference to the Mesh so that it can be serialised.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply