Avoiding extra game objects from being generated

Discussion and help for Easy Save 3
Post Reply
jayway
Posts: 3
Joined: Fri Aug 28, 2020 3:41 am

Avoiding extra game objects from being generated

Post by jayway »

Hello,

I am saving down scripts that are inherited MonoBehavior instances. Is there a way to prevent gameobjects from being created when they are loaded? Unfortunately, I've implemented too much to decouple the MonoBehavior from the scripts I want to save down.

Code: Select all

VillagerStats[] villager_stats = FindObjectsOfType<VillagerStats>();
ES3.Save<VillagerStats[]>("villager_stats", villager_stats, "current_player_profile_uid");
When I load:
VillagerStats[] villager_stats = ES3.Load<VillagerStats[]>("villager_stats_key", "current_player_profile_uid");

I use the information in VillagerStats to find the prefab and instantiate a new gameObject and then copy over the VillagerStats fields to the newly instantited VillagerStats. I find that this creates an empty GameObject for the loaded VillagerStats. VillagerStats has references to other game objects. When I load, some of those referenced game objects are also created.

I don't think I can use LoadInto since many of my objects are procedurally generated...

My solution for now is to delete all those extra game objects after everything is loaded, to avoid dups. However, I was wondering if there is a more elegant solution.

Thank you!
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: Avoiding extra game objects from being generated

Post by Joel »

Hi there,

As a Component cannot exist without a GameObject to attach it to, Easy Save creates these GameObjects. In your case you would need to use ES3.LoadInto to tell Easy Save what instance you want to load the data into so that it doesn't need to create a GameObject.

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