Page 1 of 1

Saving state of gameObject

Posted: Thu Jun 01, 2023 9:12 am
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!

Re: Saving state of gameObject

Posted: Thu Jun 01, 2023 9:25 am
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

Re: Saving state of gameObject

Posted: Thu Jun 01, 2023 9:32 am
by Marcus79
Holy crap! Thanks for the fast reply! I tried your script and it works like a charm! Thank you very much!!

Re: Saving state of gameObject

Posted: Thu Jun 01, 2023 9:55 am
by Joel
Glad that's working well for you, let me know if you run into any other issues :)

All the best,
Joel