Saving an object's SetActive value?
-
- Posts: 2
- Joined: Wed Jul 19, 2017 12:01 pm
Saving an object's SetActive value?
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?
Re: Saving an object's SetActive value?
This is indeed possible. Find below an example of a script which does this:
All the best,
Joel
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
-
- Posts: 2
- Joined: Wed Jul 19, 2017 12:01 pm
Re: Saving an object's SetActive value?
Excellent, thank you, it works perfectly.