Saving an object's SetActive value?

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
GazCarbonique
Posts: 2
Joined: Wed Jul 19, 2017 12:01 pm

Saving an object's SetActive value?

Post by GazCarbonique »

Is it possible to save an object's value for GameObject.SetActive? For example, if the value is changed from "true" to "false" during runtime, can that value be saved and loaded?
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving an object's SetActive value?

Post by Joel »

This is indeed possible. Find below an example of a script which does this:
using UnityEngine;

public class SaveActiveState : MonoBehaviour
{
    public void OnDestroy()
    {
        // Save the enabled state when the scene/object is destroyed.
        ES2.Save(gameObject.activeSelf, "myFile.txt?tag=active" + gameObject.name);
    }

    public void Start()
    {
        // Check if there's an active state to load. If there is, load our active state.
        if(ES2.Exists("myFile.txt?tag=active" + gameObject.name))
            gameObject.SetActive( ES2.Load<bool>( "myFile.txt?tag=active" + gameObject.name ) );
    }
}
Note that this assumes that the GameObject's name is unique. If it's not, you can replace gameObject.name with your own unique ID.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
GazCarbonique
Posts: 2
Joined: Wed Jul 19, 2017 12:01 pm

Re: Saving an object's SetActive value?

Post by GazCarbonique »

Excellent, thank you, it works perfectly.
Locked