Saving state of gameObject

Discussion and help for Easy Save 3
Post Reply
Marcus79
Posts: 7
Joined: Thu Jun 01, 2023 9:01 am

Saving state of gameObject

Post by Marcus79 »

Hi guys!

I just purchased Easy Save 3 yesterday and reading all the documentation that came with it I still have a few questions. I've been using PlayerPrefs thus far (not too happy with it, that's why I got ES3) and from what I've seen so far ES3 is a way better version of it including some other extra features. I'm still really new when it comes to any saving system.

But what I don't really understand is how gameObjects (prefabs) are saved within Unity. I work with multiple scenes, and one scene (house) has a key on the table that can be picked up and goes into the players inventory and the gameObject itself gets destroyed. When the player goes back outside (outside scene) I managed to save the player's inventory with ES3. But when I go back inside, the key is still on the table.

How can I use ES3 to let the key gameObject know that it has been destroyed or deactivated so it won't appear on the table again once it has been picked up previously when the player goes back into the house?

thanks in advance!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving state of gameObject

Post by Joel »

Hi there,

You can use the Save Destroyed script to save the destroyed state of a GameObject:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveDestroyed : MonoBehaviour 
{
	public string guid = System.Guid.NewGuid().ToString();
    static bool isApplicationQuitting = false;

	void Start () 
	{
		if(ES3.KeyExists(guid))
			Destroy(this.gameObject);
	}

	void OnDestroy()
	{
        if (gameObject.scene.isLoaded && !isApplicationQuitting)
            ES3.Save<bool>(guid, true);
	}

    void OnApplicationQuit()
    {
        isApplicationQuitting = true;
    }
}
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Marcus79
Posts: 7
Joined: Thu Jun 01, 2023 9:01 am

Re: Saving state of gameObject

Post by Marcus79 »

Holy crap! Thanks for the fast reply! I tried your script and it works like a charm! Thank you very much!!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving state of gameObject

Post by Joel »

Glad that's working well for you, let me know if you run into any other issues :)

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