Load gameobject ignoring instance id

Discussion and help for Easy Save 3
Post Reply
tyronleo
Posts: 2
Joined: Fri May 10, 2019 11:20 pm

Load gameobject ignoring instance id

Post by tyronleo »

Hi. I just purchade EasySave hours ago.
I wrote a little script to save and load a gameobject that have a script with an scriptable object and some other values.
The save is working good, but when I try to "loadinto" another gameobject (B), it doesn't load the script component attached, but change the name of the gameobject (B) and the tag, and it load the information in the main gameobject.
If i delete the main gameobject, EasySave creates a new gameobject called "Easy Save 3 Loaded GameObject", instead of loading the information in the gameobject (B) i use as an argument in the loadinto function.

The Save and load script is this:

Code: Select all

public class Manager : MonoBehaviour
	{
        GameObject player;
        GameObject newPlayer;
        
		void Start()
		{
            player = GameObject.FindGameObjectWithTag("LeoPlayer");
            newPlayer = GameObject.FindGameObjectWithTag("NewPlayer");
		}

        public void LeoSave() {
            ES3.Save<GameObject>("leoPlayer", player);
            Debug.Log("Saved");
        }

        public void LeoLoad() {
            ES3.LoadInto<GameObject>("leoPlayer", newPlayer);
            Debug.Log("Loaded");
        }
    }
The thing i need is to save the script component information in the gameobject and load it in any other gameobject, even in other scene. How to do that?
Any help please? Thanks in advance.
User avatar
Joel
Moodkie Staff
Posts: 4848
Joined: Wed Nov 07, 2012 10:32 pm

Re: Load gameobject ignoring instance id

Post by Joel »

Hi there,

When loading GameObjects, it's Components are resolved by their reference ID because it's not possible to determine what data belongs to which Component any other way.

If you want to save a GameObject and apply that data to a different GameObject, you will need to save the Components separately and use LoadInto to load the data into the specific Component of the other GameObject.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
tyronleo
Posts: 2
Joined: Fri May 10, 2019 11:20 pm

Re: Load gameobject ignoring instance id

Post by tyronleo »

Thanks a lot :D

I solved it doing this, I don't know if this is the right way:

Code: Select all

public void LeoSave() {
	ES3.Save<Recruit>("leoPlayer", player.GetComponent<Recruit>());
	Debug.Log("Saved");
}

public void LeoLoad() {
	ES3.LoadInto<Recruit>("leoPlayer", newPlayer.AddComponent<Recruit>());
	Debug.Log("Loaded");
}
User avatar
Joel
Moodkie Staff
Posts: 4848
Joined: Wed Nov 07, 2012 10:32 pm

Re: Load gameobject ignoring instance id

Post by Joel »

Good to hear, that is indeed how I would do it :)

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