Certain Prefabs not instantiating child objects

Discussion and help for Easy Save 3
Post Reply
Keeko
Posts: 17
Joined: Sun Nov 15, 2020 9:18 pm

Certain Prefabs not instantiating child objects

Post by Keeko »

I have been preparing some of my prefabs by right clicking them and then marking them for easy save. I do this for my town buildings. At the start of the scene the town buildings are then loaded into a list of game objects, this causes the prefabs to be instantiated with all the correct children (mesh renderers, colliders etc). However, after this step i do the same for some farm animals which are also marked as save prefab, they are also loaded into a list of gameobjects, but only their top level object is being loaded in, the children are not.

This issue is a bit confusing to me because when i open the save data to see the townbuildings vs the animals, neither of them store references to their children. I am assuming this is because it doesn't need to save them if it plans to instantiate the prefab at runtime. So this doesn't explain why the chickens dont have their child objects. I can solve this by putting autosave component on them with save children, but this saves a lot of unnecessary data to the save file and still doesn't answer this inconsistency.

for reference, this is the load routine in my TownManager:

Code: Select all

    private void LoadEventHandler(EventQueueType eventQueueType, string eventParameter, int quantity)
    {
        //We only need to load player town builders if the player is actually in their town scene
        if (LevelManager.instance.ActiveSceneName != "Overworld_PlayerTown") 
            return;

        if(ES3.KeyExists("PlayerTown_BuildingsGameObjects"))
        {
            TownBuildingGameobjects = ES3.Load("PlayerTown_BuildingsGameObjects", new List<GameObject>());
        }

        if (ES3.KeyExists("PlayerTown_Buildings"))
        {
            TownBuildings = ES3.Load("PlayerTown_Buildings", new List<TownBuilding>());
        }

        if(ES3.KeyExists("PlayerTown_NonUniqueBuildings"))
        {
            nonUniqueBuildings = ES3.Load("PlayerTown_NonUniqueBuildings", new Dictionary<string, TownBuilding>());
        }

        RebuildNPCBuildingReference();

        foreach (string guid in nonUniqueBuildings.Keys)
        {
            nonUniqueBuildings[guid].GetComponent<AnimalStructure>().LoadFromDisk();
        }
    }
and for the animal structure itself, which is responsible for loading the animals:

Code: Select all

    public void LoadFromDisk()
    {
        if (ES3.KeyExists(string.Format("{0}_{1}", structureName.Replace(" ", ""), Ref_TownBuilding.Guid)))
        {
            animalGameobjects = ES3.Load(string.Format("{0}_{1}", structureName.Replace(" ", ""), Ref_TownBuilding.Guid), new List<GameObject>());
            animalInventory.LoadInventoryFromDisk();
            foodInventory.LoadInventoryFromDisk();
            produceInventory.LoadInventoryFromDisk();
        }
    }
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Certain Prefabs not instantiating child objects

Post by Joel »

Hi there,

Please could you replicate this in a new project with a basic scene and private message it to me with instructions so I can see what is happening?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Keeko
Posts: 17
Joined: Sun Nov 15, 2020 9:18 pm

Re: Certain Prefabs not instantiating child objects

Post by Keeko »

That might take some time due to the level of coupling that i currently have, is it possible to confirm that marking prefabs as saveable should instantiate the full object on load and not just the top level one?
Keeko
Posts: 17
Joined: Sun Nov 15, 2020 9:18 pm

Re: Certain Prefabs not instantiating child objects

Post by Keeko »

Another question i have in regards to this. If i have an easysave prefab that has child easysave prefabs that are added at runtime. Does saving the root easysave prefab also save and instantiate the children?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Certain Prefabs not instantiating child objects

Post by Joel »

Keeko wrote: Thu Apr 15, 2021 11:00 am That might take some time due to the level of coupling that i currently have, is it possible to confirm that marking prefabs as saveable should instantiate the full object on load and not just the top level one?
That is correct. We simply call GameObject.Instantiate at our end, and then load the data into this, so if it's only returning the top-level of the prefab then this suggests a bug at Unity's end.
If i have an easysave prefab that has child easysave prefabs that are added at runtime. Does saving the root easysave prefab also save and instantiate the children?
As long as 'Save GameObject Children' in checked in Tools > Easy Save 3 > Settings, a reference to each of these prefabs exist in the scene prior to runtime and they all have ES3Prefab Components attached, then this is correct.

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