[EASYSAVE3] Struggling to save gameobject list

Discussion and help for Easy Save 3
Post Reply
Gee85
Posts: 10
Joined: Sun Oct 01, 2017 9:34 pm

[EASYSAVE3] Struggling to save gameobject list

Post by Gee85 »

Hi!

Sorry for sounding noobish if I do, however I am quite good at using Easy Save 2 and love how easy it actually is to use. Im just having an issue with Easy Save 3 saving a gameobject list... It's probably something I am doing wrong, and probably something very simple! However I shall paste the code below for you to point out my errors if you can =)

Code: Select all

public class ES3_Save : MonoBehaviour
{

    public void Save()
    {
        List<GameObject> bricks = new List<GameObject>(GameObject.FindGameObjectsWithTag("Player"));
        
        for (int i = 0; i < bricks.Count; i++)
        {
            ES3.Save<GameObject>("go" + i.ToString(), bricks, "brickInfo.dat");
            Debug.Log("go" + i.ToString());
        }
        ES3.Save<int>("count", bricks.Count, "brickInfo.dat");
    }
}
And the loading code is:

Code: Select all

public class ES3_Load : MonoBehaviour
{
    public void Load()
    {
        int count = ES3.Load<int>("count", "brickInfo.dat");
        List<GameObject> bricks = new List<GameObject>();
        for(int i = 0; i < count; i ++)
        {            
            bricks.Add(ES3.Load<GameObject>("go" + i, "brickInfo.dat"));
            Debug.Log("go" + i.ToString());
        }
        
    }
}
When I try to use this code with only the debug.log I get two seperate lists showing go0, go1 and go2 When I try to load the gameobjects I get the following:
InvalidCastException: Cannot cast from source type to destination type.
Code also doesn't work for a single gameobject (I can get a single gameobject to save and load but not this way :P) Thanks in advance for any advice / pointers =)
Gee85
Posts: 10
Joined: Sun Oct 01, 2017 9:34 pm

Re: [EASYSAVE3] Struggling to save gameobject list

Post by Gee85 »

Whoopsy! I found the error so I shall leave this here for anyone else who may make the same mistake.

The fault lies in the highlighted line:

public class ES3_Save : MonoBehaviour
{

public void Save()
{
List<GameObject> bricks = new List<GameObject>(GameObject.FindGameObjectsWithTag("Player"));

for (int i = 0; i < bricks.Count; i++)
{
ES3.Save<GameObject>("go" + i.ToString(), bricks, "brickInfo.dat");
Debug.Log("go" + i.ToString());
}
ES3.Save<int>("count", bricks.Count, "brickInfo.dat");

It should read ES3.Save<GameObject>("go" + i.ToString(), bricks, "brickInfo.dat");

Notice the after bricks to indicate which gameobject from the list is to be fed in under the current tag! Sorry for being noob, and hope this helps anyone having the same issues =)
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: [EASYSAVE3] Struggling to save gameobject list

Post by Joel »

Hi there,

Glad you managed to find a solution to your problem!

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